Metric LinesΒΆ

This example demonstrates how classes inside of the metric package metric can be used to compute values which describe the relationship between to lines and points. The relationship between other shapes can also be done using the same classes.

ExampleMetricLine.java

 1    public static void distancePointToLine() {
 2        System.out.println("Distance of points to lines");
 3        // two points which are used to define a line
 4        Point2D_F64 a = new Point2D_F64(0,0);
 5        Point2D_F64 b = new Point2D_F64(10,10);
 6        // a point on the line
 7        Point2D_F64 c = new Point2D_F64(20,20);
 8        // a point off the line
 9        Point2D_F64 d = new Point2D_F64(5,8);
10 
11        // define a line segment using two points
12        LineSegment2D_F64 ls = new LineSegment2D_F64(a,b);
13        // convert the line segment into a parametric line
14        LineParametric2D_F64 lp = new LineParametric2D_F64(a.x,a.y,b.x-a.x,b.y-a.y);
15 
16        // Point C should lie along the infinite line but be outside of the finite line segment
17        System.out.println("Distance of line segment to point c     : "+ Distance2D_F64.distance(ls,c));
18        System.out.println("Distance of parametric line to point c  : "+ Distance2D_F64.distance(lp,c));
19        // Point D should be the same distance from both lines
20        System.out.println("Distance of line segment to point d     : "+ Distance2D_F64.distance(ls,d));
21        System.out.println("Distance of parametric line to point d  : "+ Distance2D_F64.distance(lp,d));
22    }
23 
24    public static void intersectionLineSegment() {
25        LineSegment2D_F64 lineA = new LineSegment2D_F64(-10,-10,10,10);
26        LineSegment2D_F64 lineB = new LineSegment2D_F64(-10,10,10,-10);
27        LineSegment2D_F64 lineC = new LineSegment2D_F64(-10,-9,10,11);
28        LineSegment2D_F64 lineD = new LineSegment2D_F64(-20,-20,-18,-18);
29        LineSegment2D_F64 lineE = new LineSegment2D_F64(-20,-20,-9,-9);
30 
31        System.out.println("Intersection between line segments");
32        System.out.println("A and B (one)  : "+ Intersection2D_F64.intersection(lineA,lineB,null));
33        System.out.println("A and C (none) : "+ Intersection2D_F64.intersection(lineA,lineC,null));
34        System.out.println("A and D (none) : "+ Intersection2D_F64.intersection(lineA,lineD,null));
35        System.out.println("A and E (many) : "+ Intersection2D_F64.intersection(lineA,lineE,null));
36 
37    }
38 
39    public static void intersectionParametric() {
40        LineParametric2D_F64 lineA = new LineParametric2D_F64(0,0,1,1);
41        LineParametric2D_F64 lineB = new LineParametric2D_F64(0,0,1,-1);
42        LineParametric2D_F64 lineC = new LineParametric2D_F64(1,0,1,1);
43        LineParametric2D_F64 lineD = new LineParametric2D_F64(-1,-1,1,1);
44 
45        System.out.println("Intersection between lines");
46        System.out.println("A and B (one)  : "+ Intersection2D_F64.intersection(lineA,lineB));
47        System.out.println("A and C (none) : "+ Intersection2D_F64.intersection(lineA,lineC));
48        System.out.println("A and D (many) : "+ Intersection2D_F64.intersection(lineA,lineD));
49    }
50 
51    public static void main( String args[] ) {
52        distancePointToLine();
53        System.out.println();
54        intersectionLineSegment();
55        System.out.println();
56        intersectionParametric();
57    }