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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public static void distancePointToLine() {
    System.out.println("Distance of points to lines");
    // two points which are used to define a line
    Point2D_F64 a = new Point2D_F64(0,0);
    Point2D_F64 b = new Point2D_F64(10,10);
    // a point on the line
    Point2D_F64 c = new Point2D_F64(20,20);
    // a point off the line
    Point2D_F64 d = new Point2D_F64(5,8);

    // define a line segment using two points
    LineSegment2D_F64 ls = new LineSegment2D_F64(a,b);
    // convert the line segment into a parametric line
    LineParametric2D_F64 lp = new LineParametric2D_F64(a.x,a.y,b.x-a.x,b.y-a.y);

    // Point C should lie along the infinite line but be outside of the finite line segment
    System.out.println("Distance of line segment to point c     : "+ Distance2D_F64.distance(ls,c));
    System.out.println("Distance of parametric line to point c  : "+ Distance2D_F64.distance(lp,c));
    // Point D should be the same distance from both lines
    System.out.println("Distance of line segment to point d     : "+ Distance2D_F64.distance(ls,d));
    System.out.println("Distance of parametric line to point d  : "+ Distance2D_F64.distance(lp,d));
}

public static void intersectionLineSegment() {
    LineSegment2D_F64 lineA = new LineSegment2D_F64(-10,-10,10,10);
    LineSegment2D_F64 lineB = new LineSegment2D_F64(-10,10,10,-10);
    LineSegment2D_F64 lineC = new LineSegment2D_F64(-10,-9,10,11);
    LineSegment2D_F64 lineD = new LineSegment2D_F64(-20,-20,-18,-18);
    LineSegment2D_F64 lineE = new LineSegment2D_F64(-20,-20,-9,-9);

    System.out.println("Intersection between line segments");
    System.out.println("A and B (one)  : "+ Intersection2D_F64.intersection(lineA,lineB,null));
    System.out.println("A and C (none) : "+ Intersection2D_F64.intersection(lineA,lineC,null));
    System.out.println("A and D (none) : "+ Intersection2D_F64.intersection(lineA,lineD,null));
    System.out.println("A and E (many) : "+ Intersection2D_F64.intersection(lineA,lineE,null));

}

public static void intersectionParametric() {
    LineParametric2D_F64 lineA = new LineParametric2D_F64(0,0,1,1);
    LineParametric2D_F64 lineB = new LineParametric2D_F64(0,0,1,-1);
    LineParametric2D_F64 lineC = new LineParametric2D_F64(1,0,1,1);
    LineParametric2D_F64 lineD = new LineParametric2D_F64(-1,-1,1,1);

    System.out.println("Intersection between lines");
    System.out.println("A and B (one)  : "+ Intersection2D_F64.intersection(lineA,lineB));
    System.out.println("A and C (none) : "+ Intersection2D_F64.intersection(lineA,lineC));
    System.out.println("A and D (many) : "+ Intersection2D_F64.intersection(lineA,lineD));
}

public static void main( String args[] ) {
    distancePointToLine();
    System.out.println();
    intersectionLineSegment();
    System.out.println();
    intersectionParametric();
}