...

Source file src/oss.terrastruct.com/d2/lib/geo/ellipse_test.go

Documentation: oss.terrastruct.com/d2/lib/geo

     1  package geo
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  )
     7  
     8  func TestEllipseLineIntersections(t *testing.T) {
     9  	e := NewEllipse(NewPoint(0, 0), 11, 11)
    10  
    11  	intersections := e.Intersections(Segment{
    12  		Start: NewPoint(0, 20),
    13  		End:   NewPoint(0, -20),
    14  	})
    15  	if len(intersections) != 2 ||
    16  		!intersections[0].Equals(NewPoint(0, 11)) ||
    17  		!intersections[1].Equals(NewPoint(0, -11)) {
    18  		t.Fatalf("vertical intersection check failed [%v,%v]", intersections[0].ToString(), intersections[1].ToString())
    19  	}
    20  
    21  	intersections = e.Intersections(Segment{
    22  		Start: NewPoint(0, 2),
    23  		End:   NewPoint(0, -2),
    24  	})
    25  	if len(intersections) != 0 {
    26  		t.Fatalf("(vertical) line intersects but segment shouldn't")
    27  	}
    28  
    29  	intersections = e.Intersections(Segment{
    30  		Start: NewPoint(2, 2),
    31  		End:   NewPoint(5, 5),
    32  	})
    33  	if len(intersections) != 0 {
    34  		t.Fatalf("line intersects but segment shouldn't")
    35  	}
    36  
    37  	intersections = e.Intersections(Segment{
    38  		Start: NewPoint(2, 2),
    39  		End:   NewPoint(50, 50),
    40  	})
    41  	x := math.Sqrt2 / 2 * 11
    42  	expected := NewPoint(x, x)
    43  	if len(intersections) != 1 || !intersections[0].Equals(expected) {
    44  		t.Fatalf("intersection check failed with %v expected %v", intersections[0].ToString(), expected.ToString())
    45  	}
    46  
    47  	// test with cx,cy offset
    48  	e = NewEllipse(NewPoint(100, 200), 21, 21)
    49  	intersections = e.Intersections(Segment{
    50  		Start: NewPoint(0, 0),
    51  		End:   NewPoint(100, 150),
    52  	})
    53  	if len(intersections) != 0 {
    54  		t.Fatalf("shouldn't intersect with offset")
    55  	}
    56  	intersections = e.Intersections(Segment{
    57  		Start: NewPoint(50, 150),
    58  		End:   NewPoint(200, 250),
    59  	})
    60  	if len(intersections) != 2 {
    61  		t.Fatalf("should intersect with offset")
    62  	}
    63  
    64  	// tangent
    65  	intersections = e.Intersections(Segment{
    66  		Start: NewPoint(0, 221),
    67  		End:   NewPoint(200, 221),
    68  	})
    69  	if len(intersections) != 1 {
    70  		t.Fatalf("should intersect horizontal tangent")
    71  	}
    72  	intersections = e.Intersections(Segment{
    73  		Start: NewPoint(121, 100),
    74  		End:   NewPoint(121, 300),
    75  	})
    76  	if len(intersections) != 1 {
    77  		t.Fatalf("should intersect vertical tangent")
    78  	}
    79  	intersections = NewEllipse(
    80  		NewPoint(1, 1),
    81  		2/math.Sqrt2,
    82  		2/math.Sqrt2,
    83  	).Intersections(Segment{
    84  		Start: NewPoint(1, 3),
    85  		End:   NewPoint(3, 1),
    86  	})
    87  	if len(intersections) == 0 {
    88  		// Note: due to floating point accuracy, there are two intersections instead of one
    89  		t.Fatalf("should intersect tangent")
    90  	}
    91  }
    92  

View as plain text