构造点,线结构

任何类型的数据结构都可以构造,只要你能对其进行抽象。

辣么,如何构造点结构与线?

—–并没有提供太多操作方法—但是可以以此继续扩展—-

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 makePoint(Number x, Number y) {
        return new Point<>(x, y);
    }

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

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

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

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

    public static Number distance(Point p, Point 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 px = LineOperator.makePoint(1, 1);
        Point 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……..

你可能感兴趣的:(Java,computer)