Geometry Utilities : Geometry « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » 2D Graphics GUI » GeometryScreenshots 
Geometry Utilities
       

/**
 * GeometryUtilities.java
 */

import static java.lang.Math.PI;
import static java.lang.Math.abs;
import static java.lang.Math.atan2;
import static java.lang.Math.sqrt;
import static java.lang.Math.toDegrees;

import java.awt.geom.Arc2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Vector;

/**
 @author Cameron Behar
 */
public class GeometryUtilities {
  public static double angle(double x, double y) {
    return -(atan2(y, x- PI / 2.0);
  }

  public static double angleBetween(double angle0, double angle1) {
    // Going counter clock-wise
    return angle1 - (angle0 + ((angle0 < angle1360.0 0.0));
  }

  public static double determinant(double[][] A) {
    if (A.length == 1)
      return A[0][0];

    if (A.length == 2)
      return A[0][0* A[1][1- A[0][1* A[1][0];

    double result = 0;
    for (int i = 0; i < A[0].length; i++) {
      double temp[][] new double[A.length - 1][A[0].length - 1];

      for (int j = 1; j < A.length; j++)
        for (int k = 0; k < A[0].length; k++)
          if (k < i)
            temp[j - 1][k= A[j][k];
          else if (k > i)
            temp[j - 1][k - 1= A[j][k];

      result += A[0][i((i & 1== : -1* determinant(temp);
    }

    return result;
  }

  public static Vector<Point2D> getCrossings(Arc2D arc0, Point2D arc0Center,
      Arc2D arc1, Point2D arc1Center) {
    Vector<Point2D> ret = new Vector<Point2D>();

    double distance = arc0Center.distance(arc1Center);
    double radius0Squared = arc0Center.distanceSq(arc0.getStartPoint());
    double radius0 = sqrt(radius0Squared);
    double radius1Squared = arc1Center.distanceSq(arc1.getStartPoint());
    double radius1 = sqrt(radius1Squared);

    if (distance > radius0 + radius1) {
      // There are no solutions because the circles are separate.
    else if (distance < abs(radius0 - radius1)) {
      // There are no solutions because one circle is contained within the
      // other.
    else if (distance == && radius0 == radius1) {
      // There are an infinite number of solutions because the circles are
      // coincident.
    else {
      // Calculate the first intersection
      double x0 = arc0Center.getX(), y0 = arc0Center.getY();
      double x1 = arc1Center.getX(), y1 = arc1Center.getY();

      double a = (radius0Squared - radius1Squared + distance * distance)
          (* distance);
      double h = sqrt(radius0Squared - a * a);

      double x2 = x0 + a * (x1 - x0/ distance;
      double y2 = y0 + a * (y1 - y0/ distance;

      Point2D.Double intersection = new Point2D.Double(x2 + h * (y1 - y0)
          / distance, y2 - h * (x1 - x0/ distance);
      double angle0ToIntersection = toDegrees(atan2(
          -(intersection.y - y0), intersection.x - x0));
      double angle1ToIntersection = toDegrees(atan2(
          -(intersection.y - y1), intersection.x - x1));
      if (arc0.containsAngle(angle0ToIntersection)
          && arc1.containsAngle(angle1ToIntersection))
        ret.add(intersection);

      // If the circles aren't tangential, calculate the second
      // intersection
      if (distance != radius0 + radius1) {
        intersection = new Point2D.Double(
            x2 - h * (y1 - y0/ distance, y2 + h * (x1 - x0)
                / distance);
        angle0ToIntersection = toDegrees(atan2(-(intersection.y - y0),
            intersection.x - x0));
        angle1ToIntersection = toDegrees(atan2(-(intersection.y - y1),
            intersection.x - x1));
        if (arc0.containsAngle(angle0ToIntersection)
            && arc1.containsAngle(angle1ToIntersection))
          ret.add(intersection);
      }
    }

    return ret;
  }

  public static Vector<Point2D> getCrossings(Line2D line0, Line2D line1) {
    Vector<Point2D> ret = new Vector<Point2D>();

    if (line0.intersectsLine(line1)) {
      Point2D.Double intersection = new Point2D.Double(0.00.0);

      double xDiff0 = line0.getX2() - line0.getX1();
      double xDiff1 = line1.getX2() - line1.getX1();
      double yDiff0 = line0.getY2() - line0.getY1();
      double yDiff1 = line1.getY2() - line1.getY1();
      double xDiff2 = line0.getX1() - line1.getX1();
      double yDiff2 = line0.getY1() - line1.getY1();

      double div = yDiff1 * xDiff0 - xDiff1 * yDiff0;
      double u = (xDiff1 * yDiff2 - yDiff1 * xDiff2/ div;
      intersection.x = line0.getX1() + u * xDiff0;
      intersection.y = line0.getY1() + u * yDiff0;

      ret.add(intersection);
    }

    return ret;
  }

  public static boolean isBetween(Point2D a, Point2D b, Point2D c) {
    double u = c.getX() - a.getX();
    double v = c.getY() - a.getY();
    double tx = (b.getX() - a.getX()) / u;
    double ty = (b.getY() - a.getY()) / v;

    return (tx > && tx < 1|| (ty > && ty < 1);
  }

  public static Point2D midpoint(double x0, double y0, double x1, double y1) {
    return new Point2D.Double((x1 + x02.0(y1 + y02.0);
  }

  public static double slope(double x0, double y0, double x1, double y1) {
    return (y1 - y0(x1 - x0);
  }

  public static double slope(Line2D line) {
    return slope(line.getX1(), line.getY1(), line.getX2(), line.getY2());
  }
}

   
    
    
    
    
    
    
  
Related examples in the same category
1.Collection of geometry utility methods
2.Unions Rectangle2D
3.Interpolates points given in the 2D plane
4.Returns distance between two sets of coords
5.Returns distance between 3D set of coords
6.Returns closest point on segment to point
7.Calculate Angle From
8.Returns distance to segment
9.Hexagon demo
10.Implements an Vector in 3D space.
11.Implementation of the 4 dimensional vector.
12.Quaternion
13.Circle shape
14.This is a Polygon that allows the user to flip and swap the points along it's axis.
15.Fast trigonometric operationsFast trigonometric operations
16.A class to represent a latitude and longitude
17.An undirected graph that keeps track of connected components (groups).
18.Generates n logarithmically-spaced points between d1 and d2 using the provided base.
19.Returns a dimension where width and height are inside the bounds of the maxWidth and maxHeight parameters
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.