Java Geometry Algorithm findArea(Point2D p1, Point2D p2, Point2D p3)

Here you can find the source of findArea(Point2D p1, Point2D p2, Point2D p3)

Description

Given three points each represents the vertex of a triangle, return the area of this triangle.

License

Open Source License

Declaration

public static double findArea(Point2D p1, Point2D p2, Point2D p3) 

Method Source Code


//package com.java2s;
import java.awt.geom.Point2D;

public class Main {
    /**//  w ww.  java 2  s.c om
     *  Given three points each represents the vertex of a triangle,
     *  return the area of this triangle.
     */
    public static double findArea(Point2D p1, Point2D p2, Point2D p3) {
        if (!preprocess(p1, p2, p3)) {
            return 0.0;
        } else {
            double p4x;
            double p4y;
            double m13 = findSlope(p1.getX(), p1.getY(), p3.getX(), p3.getY());
            double m24 = 0;
            if (Double.isNaN(m13)) {
                p4x = p2.getX();
                p4y = p1.getY();
            } else if (m13 == 0) {
                p4x = p1.getX();
                p4y = p2.getY();
            } else {
                m24 = -1 / m13;
                p4y = (p2.getX() - m24 * p2.getY() - p1.getX() + m13 * p1.getY()) / (m13 - m24);
                p4x = p2.getX() - p2.getY() * m24 + p4y * m24;
                if (findSlope(p2.getX(), p2.getY(), p4x, p4y) == m13) {
                    // 3 points on the same line
                    return 0;
                }
            }
            double dist24 = Math.sqrt(Math.pow((p2.getX() - p4x), 2) + Math.pow((p2.getY() - p4y), 2));
            double dist13 = Math.sqrt(Math.pow((p1.getX() - p3.getX()), 2) + Math.pow((p1.getY() - p3.getY()), 2));
            double area = 0.5 * dist13 * dist24;
            return area;
        }
    }

    /**
     * Return false when two of the points are the same, because
     * the area would be 0.
     */
    private static boolean preprocess(Point2D pa, Point2D pb, Point2D pc) {
        if ((pa.getX() == pb.getX()) && (pa.getY() == pb.getY())) {
            return false;
        } else if ((pa.getX() == pc.getX()) && (pa.getY() == pc.getY())) {
            return false;
        } else if ((pb.getX() == pc.getX()) && (pb.getY() == pc.getY())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Find the slope of the line formed by two points
     * (p1x, p1y) and (p2x, p2y).
     */
    public static double findSlope(double p1x, double p1y, double p2x, double p2y) {
        double nominator = p2x - p1x;
        double denominator = p2y - p1y;
        if (denominator == 0) {
            return Double.NaN;
        } else {
            return nominator / denominator;
        }
    }
}

Related

  1. dotNorm(final Point2D a, final Point2D b)
  2. drag(Robot robot, Point startPoint, Point endPoint, int button)
  3. dx(Point a, Point b)
  4. edge(Collection points)
  5. extendLine(Point2D p0, Point2D p1, double toLength)
  6. findDimension(Point2D[] pts)
  7. findDistancedPoint(double t, Point2D sp, Point2D c1, Point2D c2, Point2D ep)
  8. findIntersect(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
  9. findIntersection(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3, Point2D.Double p4)