import { Vector2D } from '../../../lib/graphics/Vector2D'; import * as intersections from '../../../lib/graphics/intersections'; const vector2d = Vector2D.create; describe('graphics.intersections', () => { describe('intersectIntervals', () => { const a = [22, 38]; const b1 = [4, 15]; const b2 = [11, 22]; const b3 = [15, 26]; const b4 = [15, 38]; const b5 = [15, 50]; const b6 = [22, 33]; const b7 = [22, 38]; const b8 = [22, 46]; const b9 = [25, 36]; const b10 = [25, 38]; const b11 = [25, 45]; const b12 = [38, 50]; const b13 = [41, 53]; test.each([ [a[0], a[1], b1[0], b1[1], undefined], [a[0], a[1], b2[0], b2[1], [22]], [a[0], a[1], b3[0], b3[1], [22, 26]], [a[0], a[1], b4[0], b4[1], [22, 38]], [a[0], a[1], b5[0], b5[1], [22, 38]], [a[0], a[1], b6[0], b6[1], [22, 33]], [a[0], a[1], b7[0], b7[1], [22, 38]], [a[0], a[1], b8[0], b8[1], [22, 38]], [a[0], a[1], b9[0], b9[1], [25, 36]], [a[0], a[1], b10[0], b10[1], [25, 38]], [a[0], a[1], b11[0], b11[1], [25, 38]], [a[0], a[1], b12[0], b12[1], [38]], [a[0], a[1], b13[0], b13[1], undefined], [b1[0], b1[1], a[0], a[1], undefined], [b2[0], b2[1], a[0], a[1], [22]], [b3[0], b3[1], a[0], a[1], [22, 26]], [b4[0], b4[1], a[0], a[1], [22, 38]], [b5[0], b5[1], a[0], a[1], [22, 38]], [b6[0], b6[1], a[0], a[1], [22, 33]], [b7[0], b7[1], a[0], a[1], [22, 38]], [b8[0], b8[1], a[0], a[1], [22, 38]], [b9[0], b9[1], a[0], a[1], [25, 36]], [b10[0], b10[1], a[0], a[1], [25, 38]], [b11[0], b11[1], a[0], a[1], [25, 38]], [b12[0], b12[1], a[0], a[1], [38]], [b13[0], b13[1], a[0], a[1], undefined], ])('intersectIntervals#%#', (x0: number, x1: number, y0: number, y1: number, result: number[] | undefined) => { expect(intersections.intersectIntervals(x0, x1, y0, y1)).toEqual(result); }); }); describe('intersectLinePoint', () => { test.each([ [vector2d(1, 1), vector2d(4, 4), vector2d(1, 1), [vector2d(1, 1)]], [vector2d(1, 1), vector2d(4, 4), vector2d(2, 2), [vector2d(2, 2)]], [vector2d(1, 1), vector2d(4, 4), vector2d(4, 4), [vector2d(4, 4)]], [vector2d(1, 1), vector2d(4, 4), vector2d(5, 5), undefined], [vector2d(1, 1), vector2d(4, 4), vector2d(0, 0), undefined], [vector2d(1, 1), vector2d(4, 4), vector2d(2, 1), undefined], [vector2d(1, 1), vector2d(1, 1), vector2d(2, 2), undefined], [vector2d(2, 2), vector2d(2, 2), vector2d(2, 2), [vector2d(2, 2)]] ])('intersectLinePoint#%#', (p1: Vector2D, p2: Vector2D, p3: Vector2D, result: Vector2D[] | undefined) => { expect(intersections.intersectLinePoint(p1, p2, p3)).toEqual(result); }); }); describe('intersectLineLine', () => { test.each([ [vector2d(4, 1), vector2d(8, 5), vector2d(5, 2), vector2d(7, 4), [vector2d(5, 2), vector2d(7, 4)]], [vector2d(2, 5), vector2d(6, 7), vector2d(4, 6), vector2d(6, 7), [vector2d(4, 6), vector2d(6, 7)]], [vector2d(2, 5), vector2d(6, 7), vector2d(6, 7), vector2d(4, 6), [vector2d(4, 6), vector2d(6, 7)]], [vector2d(3, 0), vector2d(5, 2), vector2d(4, 1), vector2d(8, 5), [vector2d(4, 1), vector2d(5, 2)]], [vector2d(2, 5), vector2d(6, 7), vector2d(6, 7), vector2d(8, 8), [vector2d(6, 7)]], [vector2d(5, 2), vector2d(7, 4), vector2d(4, 1), vector2d(8, 5), [vector2d(5, 2), vector2d(7, 4)]], [vector2d(1, 1), vector2d(3, 2), vector2d(3, 2), vector2d(5, 4), [vector2d(3, 2)]], [vector2d(1, 1), vector2d(3, 2), vector2d(3, 2), vector2d(5, 3), [vector2d(3, 2)]], [vector2d(2, 5), vector2d(6, 7), vector2d(3, 7), vector2d(5, 5), [vector2d(4, 6)]], [vector2d(4, 1), vector2d(8, 5), vector2d(7, 2), vector2d(9, 2), undefined], [vector2d(0, 0), vector2d(2, 8), vector2d(8, 0), vector2d(0, 20), undefined], [vector2d(0, 0), vector2d(0, 0), vector2d(2, 2), vector2d(5, 5), undefined], [vector2d(1, 1), vector2d(3, 3), vector2d(3, 1), vector2d(5, 3), undefined], [vector2d(1, 1), vector2d(3, 3), vector2d(4, 4), vector2d(6, 6), undefined], ])('intersectLineLine#%#', (p1: Vector2D, p2: Vector2D, p3: Vector2D, p4: Vector2D, result: Vector2D[] | undefined) => { expect(intersections.intersectLineLine(p1, p2, p3, p4)).toEqual(result); }); }); describe('intersectLineCircle', () => { test.each([ [vector2d(2, 6), vector2d(4, 6), vector2d(5, 5), 4, undefined], [vector2d(1, 9), vector2d(9, 9), vector2d(5, 5), 4, [vector2d(5, 9)]], [vector2d(6, 5), vector2d(10, 5), vector2d(5, 5), 4, [vector2d(9, 5)]], [vector2d(5, 0), vector2d(5, 11), vector2d(5, 5), 4, [vector2d(5, 1), vector2d(5, 9)]], [vector2d(2, 5), vector2d(2, 5), vector2d(5, 5), 3, [vector2d(2, 5)]], [vector2d(3, 5), vector2d(3, 5), vector2d(5, 5), 3, undefined], [vector2d(1, 5), vector2d(1, 5), vector2d(5, 5), 3, undefined], [vector2d(214, 289), vector2d(294, 286), vector2d(325, 227), 51, undefined], [vector2d(300, 236), vector2d(350, 224), vector2d(325, 227), 51, undefined], [vector2d(274, 300), vector2d(274, 150), vector2d(325, 227), 51, [vector2d(274, 227)]], [vector2d(265, 277), vector2d(402, 218), vector2d(325, 227), 51, [vector2d(291.6024045909439, 265.5434899936811), vector2d(375.9517527124269, 229.21785832092564)]], [vector2d(331, 218), vector2d(413, 218), vector2d(325, 227), 51, [vector2d(375.19960159204453, 218)]], [vector2d(315, 243), vector2d(325, 278), vector2d(325, 227), 51, [vector2d(325, 278)]], [vector2d(304, 180), vector2d(325, 278), vector2d(325, 227), 51, [vector2d(304.10243902439026, 180.4780487804878), vector2d(325, 278)]], [vector2d(297, 269), vector2d(275, 300), vector2d(325, 227), 51, [vector2d(296.6976032829431, 269.4261044649438)]], ])('intersectLineCircle#%#', (p1: Vector2D, p2: Vector2D, c: Vector2D, r: number, result: Vector2D[] | undefined) => { expect(intersections.intersectLineCircle(p1, p2, c, r)).toEqual(result); }); }); describe('intersectCircleCircle', () => { test.each([ [vector2d(3, 6), 3, vector2d(4, 6), 4, [vector2d(0, 6)]], [vector2d(5, 6), 3, vector2d(4, 6), 4, [vector2d(8, 6)]], [vector2d(4, 6), 3, vector2d(4, 6), 4, undefined], [vector2d(4, 6), 3, vector2d(4, 6), 3, []], [vector2d(4, 6), 3, vector2d(4, 6), 0, undefined], [vector2d(4, 6), 3, vector2d(0, 6), 0, undefined], [vector2d(-1.0, -1.0), 1.5, vector2d(1.0, 1.0), 2.0, [vector2d(-0.9361400177030619, 0.4986400177030621), vector2d(0.4986400177030621, -0.9361400177030619)]], [vector2d(1.0, -1.0), 1.5, vector2d(-1.0, 1.0), 2.0, [vector2d(-0.4986400177030621, -0.9361400177030619), vector2d(0.9361400177030619, 0.4986400177030621)]], [vector2d(-1.0, 1.0), 1.5, vector2d(1.0, -1.0), 2.0, [vector2d(0.4986400177030621, 0.9361400177030619), vector2d(-0.9361400177030619, -0.4986400177030621)]], [vector2d(1.0, 1.0), 1.5, vector2d(-1.0, -1.0), 2.0, [vector2d(0.9361400177030619, -0.4986400177030621), vector2d(-0.4986400177030621, 0.9361400177030619)]], ])('intersectCircleCircle#%#', (c1: Vector2D, r1: number, c2: Vector2D, r2: number, result: Vector2D[] | undefined) => { expect(intersections.intersectCircleCircle(c1, r1, c2, r2)).toEqual(result); }); }); });