找到给定坐标坐标的四边形区域

时间:2017-11-27 19:44:17

标签: java math user-interaction

我是一名学习Java的学生,我对我们在课堂上所做的一个例子有疑问。

练习的目的是通过用户交互获得四边形的4个顶点的(x,y)坐标,然后我们可以找到形状的区域。

我认为我的逻辑是正确的:从Scanner类开始,以启动用户交互来获取坐标,然后一旦你拥有所有4个X值和Y值,就做适当的减法来计算每一侧的长度,在这一点你可以找出该区域。

我被困在我应该保存每个顶点坐标的用户输入的地方。为每个坐标初始化不同的整数变量似乎有点过分和冗余,但我无法弄清楚他希望我们如何完成这个。

到目前为止,我的代码看起来像这样:

    import java.util.Scanner;
    public class Assign03OOP {
        public static void main (String args []) {
            Scanner userInputStream = new Scanner(System.in);
            int vertexA, vertexB, vertexC, vertexD, (x, y);
            double area, userInput;
            double [] pointA = {,}, pointB = {,}, pointC = {,}, pointD = 
                      {,};
            System.out.println("Recording Vertex A: \nEnter X Value: ");
            userInput = userInputStream.nextDouble();
            while (x != 0; y!0=;) {
                pointA = (x * y)
            }
    }
}

3 个答案:

答案 0 :(得分:0)

将四边形分解为两个三角形。三角形的面积是在两侧构造的平行四边形的面积的一半,其本身作为两个相应矢量的叉积而获得。

答案 1 :(得分:0)

另一种方法是使用Green's Theorem将区域积分转换为轮廓积分。您可以使用Gaussian quadrature沿四边形边缘进行整合以获得该区域。这适用于任何扁平多边形:三角形,四边形等。

答案 2 :(得分:0)

我认为使用实际物体代表您的点和四边形是合适的。

使用对象可以使代码更清晰,更容易测试。

package com.stackoverflow.so47518648;

import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("javadoc")
public class Answer {

    // Allow user to quit by entering Q or q
    static final Pattern SCANNER_QUIT_PATTERN = Pattern.compile("^q$", Pattern.CASE_INSENSITIVE);

    // Use of named-capturing groups, in this case 'x' and 'y' requires java 7
    static final Pattern SCANNER_INPUT_PATTERN = Pattern.compile("(?<x>\\d+),\\s*(?<y>\\d+)");

    public static void main(String... args) {

        try (Scanner sc = new Scanner(System.in)) {

            Quadrilateral quadrilateral = promptForQuadrilateral(sc);
            System.out.println("Finding area for: " + quadrilateral);

            double area = quadrilateral.area();
            System.out.println("The area of " + quadrilateral + " is :" + area);

        } catch (UserQuit userQuit) {
            System.out.println(userQuit.getMessage());
        }
    }

    static Quadrilateral promptForQuadrilateral(Scanner sc) {

        Point a = promptForPoint(sc, "A");
        Point b = promptForPoint(sc, "B");
        Point c = promptForPoint(sc, "C");
        Point d = promptForPoint(sc, "D");

        Quadrilateral q = new Quadrilateral(a, b, c, d);

        return q;
    }

    static Point promptForPoint(Scanner sc, String vertexName) {

        while (true) {

            System.out.println("Enter the coordinates for " + vertexName + " (x,y) or q to quit: ");

            String coordinates = sc.findInLine(SCANNER_INPUT_PATTERN);

            if (coordinates != null) {
                sc.nextLine();

                Matcher matcher = SCANNER_INPUT_PATTERN.matcher(coordinates);
                matcher.matches();

                int x = Integer.parseInt(matcher.group("x"));
                int y = Integer.parseInt(matcher.group("y"));

                Point point = new Point(x, y);

                System.out.println("For " + vertexName + " you entered: " + point);

                return point;
            } else if (sc.findInLine(SCANNER_QUIT_PATTERN) != null) {
                throw new UserQuit();
            } else {
                // Reprompt when invalid input is received
                sc.next();
            }

        }
    }

    public static class UserQuit extends RuntimeException {
        public UserQuit() {
            super("User quit.");
        }
    }

    public static class DuplicatePoints extends RuntimeException {
        public DuplicatePoints(Point... points) {
            super("Cannot create quadrilaterl with duplicate points: " + Arrays.toString(points));
        }
    }

    public static class Point {

        public final int x;
        public final int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        double distance(Point p1, Point p2) {
            return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
        }

        /**
         * Distance from this point to the supplied point.
         *
         * @param that the that
         * @return the double
         */
        public double distance(Point that) {

            return distance(this, that);
        }

        @Override
        public String toString() {
            return "(x=" + this.x + ", y=" + this.y + ")";
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + this.x;
            result = prime * result + this.y;
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

    }

    public static class Quadrilateral {

        public final Point a;
        public final Point b;
        public final Point c;
        public final Point d;

        public Quadrilateral(Point a, Point b, Point c, Point d) {

            validatePoints(a, b, c, d);

            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
        }

        static void validatePoints(Point a, Point b, Point c, Point d) {

            // throw exceptions if we can't construct a quadrilateral (duplicate points, etc)

            // Duplicate points check
            Point[] points = { a, b, c, d };
            if (Arrays.stream(points)
                .distinct()
                .count() != 4) {
                throw new DuplicatePoints(points);
            }

        }

        static double calulateArea(Quadrilateral quadrilateral) {

            return calulateArea(quadrilateral.a, quadrilateral.b, quadrilateral.c, quadrilateral.d);
        }

        static double calulateArea(Point a, Point b, Point c, Point d) {

            // do calculation here.
            return 0.0;

        }

        public double area() {
            return calulateArea(this);
        }

        @Override
        public String toString() {
            return "a=" + this.a + ", b=" + this.b + ", c=" + this.c + ", d=" + this.d;
        }

    }

}