### Author Topic: Ellipse Intersecting Line  (Read 888 times)

0 Members and 1 Guest are viewing this topic.

#### The Librarian

• Moderator
• Newbie
• Posts: 42
##### Ellipse Intersecting Line
« on: March 05, 2020, 11:42:02 PM »
Ellipse Intersecting Line

Author: @STxAxTIC
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=2143.0
Version: 2020
Tags: [2d], [geometry], [intersections]

Description:
In response to

https://www.qb64.org/forum/index.php?topic=2132

... all I could think is "why stop at circles when you can do ellipses?"

Source Code:
Code: QB64: [Select]
1.
2. xorig = 0
3. yorig = 0
4.
5. CALL cline(xorig, yorig, xorig + _WIDTH, yorig, 8)
6. CALL cline(xorig, yorig, xorig + -_WIDTH, yorig, 8)
7. CALL cline(xorig, yorig, xorig, yorig + _HEIGHT, 8)
8. CALL cline(xorig, yorig, xorig, yorig - _HEIGHT, 8)
9.
10. xzoom = 20
11. yzoom = 20
12.
13. ' Initialize line
14. b = -2
15. d = 2
16. lineang = .1
17. vx = COS(lineang)
18. vy = SIN(lineang)
19. m = vy / vx
20.
21. ' Initialize ellipse
22. x0 = 2
23. y0 = -2
24. ellipsearg = .2
25. amag = 10
26. ax = amag * COS(ellipsearg)
27. ay = amag * SIN(ellipsearg)
28. bmag = 5
29. bx = bmag * COS(ellipsearg + 3.14 / 2)
30. by = bmag * SIN(ellipsearg + 3.14 / 2)
31.
32.
33.         x = _MOUSEX
34.         y = _MOUSEY
35.         IF ((x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT)) THEN
36.                 x = _MOUSEX
37.                 y = _MOUSEY
38.                 x0 = (x - _WIDTH / 2) / xzoom
39.                 y0 = (-y + _HEIGHT / 2) / yzoom
40.                 x = _MOUSEX
41.                 y = _MOUSEY
42.                 d = (x - _WIDTH / 2) / xzoom
43.                 b = (-y + _HEIGHT / 2) / yzoom
44.                 lineang = lineang + .01
45.                 vx = COS(lineang)
46.                 vy = SIN(lineang)
47.                 m = vy / vx
48.                 lineang = lineang - .01
49.                 vx = COS(lineang)
50.                 vy = SIN(lineang)
51.                 m = vy / vx
52.
53.         CASE 18432
54.             bmag = bmag + .1
55.             bx = bmag * COS(ellipsearg + 3.14 / 2)
56.             by = bmag * SIN(ellipsearg + 3.14 / 2)
57.         CASE 20480
58.             bmag = bmag - .1
59.             bx = bmag * COS(ellipsearg + 3.14 / 2)
60.             by = bmag * SIN(ellipsearg + 3.14 / 2)
61.         CASE 19200
62.             ellipsearg = ellipsearg - .1
63.             ax = amag * COS(ellipsearg)
64.             ay = amag * SIN(ellipsearg)
65.             bx = bmag * COS(ellipsearg + 3.14 / 2)
66.             by = bmag * SIN(ellipsearg + 3.14 / 2)
67.         CASE 19712
68.             ellipsearg = ellipsearg + .1
69.             ax = amag * COS(ellipsearg)
70.             ay = amag * SIN(ellipsearg)
71.             bx = bmag * COS(ellipsearg + 3.14 / 2)
72.             by = bmag * SIN(ellipsearg + 3.14 / 2)
73.
74.     ' Intersections
75.     a2 = ax ^ 2 + ay ^ 2
76.     b2 = bx ^ 2 + by ^ 2
77.     av = ax * vx + ay * vy
78.     bv = bx * vx + by * vy
79.     rbx = d - x0
80.     rby = b - y0
81.     adbr = ax * rbx + ay * rby
82.     bdbr = bx * rbx + by * rby
83.     aa = av ^ 2 / a2 ^ 2 + bv ^ 2 / b2 ^ 2
84.     bb = 2 * (av * adbr / a2 ^ 2 + bv * bdbr / b2 ^ 2)
85.     cc = adbr ^ 2 / a2 ^ 2 + bdbr ^ 2 / b2 ^ 2 - 1
86.     arg = bb ^ 2 - 4 * aa * cc
87.     IF (arg > 0) THEN
88.         alpha1 = (-bb + SQR(arg)) / (2 * aa)
89.         alpha2 = (-bb - SQR(arg)) / (2 * aa)
90.         x1 = alpha1 * vx + d
91.         x2 = alpha2 * vx + d
92.         y1 = alpha1 * vy + b
93.         y2 = alpha2 * vy + b
94.         x1 = -999
95.         y1 = -999
96.         x2 = -999
97.         y2 = -999
98.
99.     GOSUB draweverything
100.
101.     _LIMIT 60
102.
103.
104. draweverything:
105. PAINT (1, 1), 15
106. COLOR 0, 15
107. LOCATE 1, 1: PRINT "LClick=Move ellipse, RClick=Move line, Scroll=Tilt line, Arrows=Shift ellipse"
108. FOR alpha = -20 TO 20 STEP .001
109.     x = alpha * vx + d
110.     y = alpha * vy + b
111.     CALL ccircle(xorig + x * xzoom, yorig + y * yzoom, 1, 1)
112. FOR t = 0 TO 6.284 STEP .001
113.     x = x0 + ax * COS(t) + bx * SIN(t)
114.     y = y0 + ay * COS(t) + by * SIN(t)
115.     CALL ccircle(xorig + x * xzoom, yorig + y * yzoom, 1, 4)
116. CALL ccircle(xorig + x1 * xzoom, yorig + y1 * yzoom, 10, 1)
117. CALL ccircle(xorig + x2 * xzoom, yorig + y2 * yzoom, 10, 1)
118.
119. SUB cline (x1, y1, x2, y2, col)
120.     LINE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2)-(_WIDTH / 2 + x2, -y2 + _HEIGHT / 2), col
121.
122. SUB ccircle (x1, y1, r, col)
123.     CIRCLE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2), r, col
124.