構造點,線結構

南郭竽發表於2018-06-10

任何型別的資料結構都可以構造,只要你能對其進行抽象。

辣麼,如何構造點結構與線?

—–並沒有提供太多操作方法—但是可以以此繼續擴充套件—-

Java實現:

package com.pycat.simple.twoDimension;

import javafx.util.Pair;

/**
 * Created by cat on 2018/6/10.
 * 點
 */
public class Point<K, V> extends Pair<K, V> {
    /**
     * Creates a new pair
     *
     * @param key   The key for this pair
     * @param value The value to use for this pair
     */
     Point(K key, V value) {
        super(key, value);
    }

    @Override
    public String toString() {
        return String.format("Point(%s,%s)", getKey(), getValue());
    }
}
package com.pycat.simple.twoDimension;

import javafx.util.Pair;

/**
 * Created by cat on 2018/6/10.
 * 線
 */
public class Line<K, V> extends Pair<K, V> {
    /**
     * Creates a new pair
     *
     * @param key   The key for this pair
     * @param value The value to use for this pair
     */
    Line(K key, V value) {
        super(key, value);
    }

    @Override
    public String toString() {
        return String.format("Line(%s,%s)", getKey(), getValue());
    }
}
package com.pycat.simple.twoDimension;

/**
 * Created by cat on 2018/6/10.
 * 點,線操作
 */
public class LineOperator {

    private LineOperator() {
    }

    public static Point<Number, Number> makePoint(Number x, Number y) {
        return new Point<>(x, y);
    }

    public static Line<Point<Number, Number>, Point<Number, Number>>
    makeLine(Point<Number, Number> p, Point<Number, Number> q) {
        return new Line<>(p, q);
    }

    public static Number dx(Point<Number, Number> p, Point<Number, Number> q) {

        return q.getKey().longValue() - p.getKey().longValue();
    }

    public static Number dy(Point<Number, Number> p, Point<Number, Number> q) {
        return q.getValue().longValue() - p.getValue().longValue();
    }

    public static Number distance(Point<Number, Number> p, Point<Number, Number> q) {
        return Math.sqrt(dx(p, q).longValue() * dx(p, q).longValue() * 1.0 +
                dy(p, q).longValue() * dy(p, q).longValue() * 1.0);
    }

    public static void main(String[] args) {
        Point<Number, Number> px = LineOperator.makePoint(1, 1);
        Point<Number, Number> py = LineOperator.makePoint(4, 5);
        System.out.println("dx=" + dx(px, py));
        System.out.println("dy=" + dy(px, py));
        System.out.println("length=" + distance(px, py));
    }
}

也許之前對程式設計有什麼誤解,也許現在對程式設計有誤解,也許一直都有誤解。

2333333333……..

相關文章