January 19, 2021, 06:03:43 AM

### Author Topic: [bmx] 2D vector library by Pineapple [ 1+ years ago ]  (Read 418 times)

#### BlitzBot

• Jr. Member
•  • Posts: 1 ##### [bmx] 2D vector library by Pineapple [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : 2D vector library
Author : Pineapple
Posted : 1+ years ago

Description : This type is loosely based on the Chipmunk library's cpVect class.

Code :
Code: BlitzMax
1. '       --+-----------------------------------------------------------------------------------------+--
2. '         |   This code was originally written by Sophie Kirschner (sophiek@pineapplemachine.com)   |
3. '         | It is released as public domain. Please don't interpret that as liberty to claim credit |
4. '         |   that isn't yours, or to sell this code when it could otherwise be obtained for free   |
5. '         |                because that would be a really shitty thing of you to do.                |
6. '       --+-----------------------------------------------------------------------------------------+--
7.
8. SuperStrict
9.
10. Import brl.math
11.
12. ' example program
13. Rem
14. Graphics 256,192
15. Local center:vect2d=vect2d.Create(128,96)
16. Local mouse:vect2d=vect2d.Create(0,0)
17. Repeat
18.         Cls
19.
20.         mouse.set MouseX(),MouseY()
21.
22.         SetColor 0,180,255
23.         Local angle:vect2d=vect2d.forangle(center.angleto(mouse))
24.         angle.applyscale(16)
25.         Local angleperp1:vect2d=angle.perp()
26.         Local angleperp2:vect2d=angle.rperp()
30.         DrawLine center.x,center.y,angle.x,angle.y
31.         SetColor 255,70,50
32.         DrawLine center.x,center.y,angleperp1.x,angleperp1.y
33.         SetColor 0,220,0
34.         DrawLine center.x,center.y,angleperp2.x,angleperp2.y
35.
36.         Flip
37. Until KeyDown(27) Or AppTerminate()
38. EndRem
39.
40. Rem
41. bbdoc: 2D vector type using doubles and with useful math functions.
42. EndRem
43. Type vect2d
44.         Field x!,y!
45.         Rem
46.         bbdoc: Returns a new vect2d with the specified x,y values.
47.         EndRem
48.         Function Create:vect2d(x!=0,y!=0)
49.                 Local n:vect2d=New vect2d
50.                 n.x=x;n.y=y
51.                 Return n
52.         End Function
53.         Rem
54.         bbdoc: Returns a copy of this vect2d.
55.         EndRem
56.         Method copy:vect2d()
57.                 Return Create(x,y)
58.         End Method
59.         Rem
60.         bbdoc: Sets this vect2d's values to the given ones.
61.         EndRem
62.         Method set(_x!,_y!)
63.                 x=_x;y=_y
64.         End Method
65.         Rem
66.         bbdoc: Sets this vect2d's values to the given vect2d's values.
67.         EndRem
68.         Method setto(o:vect2d)
69.                 x=o.x;y=o.y
70.         End Method
71.         Rem
72.         bbdoc: Sets this vect2d's values to zero.
73.         EndRem
74.         Method setzero()
75.                 x=0;y=0
76.         End Method
77.         Rem
78.         bbdoc: Returns True if the magnitude of this vect2d is zero, False otherwise.
79.         EndRem
80.         Method iszero%()
81.                 Return x=0 And y=0
82.         End Method
83.         Rem
84.         bbdoc: Returns True if this vect2d is equivalent to another, false otherwise.
85.         about: Considers differences in the x and y components that are less than or equal to the threshold (scaled epsilon) as being equal.
86.         EndRem
87.         Method equals%(o:vect2d,epsilon!=1.0e-12)
88.                 Return Abs(x-o.x)<Abs(x*epsilon) And Abs(y-o.y)<Abs(y*epsilon)
89.         End Method
90.         Rem
91.         bbdoc: Returns True if this vect2d is exactly equivalent to another, false otherwise.
92.         about: May cause errors due to imprecisions.
93.         EndRem
94.         Method equalsexact%(o:vect2d)
95.                 Return x=o.x And y=o.y
96.         End Method
97.         Rem
98.         bbdoc: Returns 1 if this vect2d is greater than another, 0 if they are equal, -1 otherwise.
100.         EndRem
101.         Method comparevects%(o:vect2d)
102.                 Local thisl!=lengthsq()
103.                 Local thatl!=o.lengthsq()
104.                 If thisl>thatl
105.                         Return 1
106.                 ElseIf thisl=thatl
107.                         Return 0
108.                 Else
109.                         Return -1
110.                 EndIf
111.         End Method
112.         Rem
113.         bbdoc: Returns the sum of this and another vect2d.
114.         EndRem
116.                 Return Create(x+o.x,y+o.y)
117.         End Method
118.         Rem
119.         bbdoc: Returns a vect2d which has the sum of the x values of two vect2ds and keeps the original y.
120.         EndRem
122.                 Return Create(x+o.x,y)
123.         End Method
124.         Rem
125.         bbdoc: Returns a vect2d which has the sum of the y values of two vect2ds and keeps the original x.
126.         EndRem
128.                 Return Create(x,y+o.y)
129.         End Method
130.         Rem
131.         bbdoc: Adds another vect2d to this one.
132.         EndRem
134.                 x:+o.x;y:+o.y
135.         End Method
136.         Rem
137.         bbdoc: Adds the x value of another vect2d to this one.
138.         EndRem
140.                 x:+o.x
141.         End Method
142.         Rem
143.         bbdoc: Adds the y value of another vect2d to this one.
144.         EndRem
146.                 y:+o.y
147.         End Method
148.         Rem
149.         bbdoc: Returns the vect2d that is the subtraction of a vect2d from this one.
150.         EndRem
151.         Method sub:vect2d(o:vect2d)
152.                 Return Create(x-o.x,y-o.y)
153.         End Method
154.         Rem
155.         bbdoc: Subtracts a vect2d from this one.
156.         EndRem
157.         Method applysub(o:vect2d)
158.                 x:-o.x;y:-o.y
159.         End Method
160.         Rem
161.         bbdoc: Returns the product of two vect2ds.
162.         EndRem
163.         Method mult:vect2d(o:vect2d)
164.                 Return Create(x*o.x,y*o.y)
165.         End Method
166.         Rem
167.         bbdoc: Multiplies this vect2d by another.
168.         EndRem
169.         Method applymult:vect2d(o:vect2d)
170.                 x:*o.x;y:*o.y
171.         End Method
172.         Rem
173.         bbdoc: Returns the negation of this vect2d.
174.         EndRem
175.         Method negate:vect2d()
176.                 Return Create(-x,-y)
177.         End Method
178.         Rem
179.         bbdoc: Negates this vect2d.
180.         EndRem
181.         Method applynegate()
182.                 x=-x;y=-y
183.         End Method
184.         Rem
185.         bbdoc: Returns the vect2d that has this one's negated x and original y.
186.         EndRem
187.         Method negatex:vect2d()
188.                 Return Create(-x,y)
189.         End Method
190.         Rem
191.         bbdoc: Returns the vect2d that has this one's original x and negated y.
192.         EndRem
193.         Method negatey:vect2d()
194.                 Return Create(x,-y)
195.         End Method
196.         Rem
197.         bbdoc: Negates this vect2d's x value.
198.         EndRem
199.         Method applynegatex()
200.                 x=-x
201.         End Method
202.         Rem
203.         bbdoc: Negates this vect2d's y value.
204.         EndRem
205.         Method applynegatey()
206.                 y=-y
207.         End Method
208.         Rem
209.         bbdoc: Returns the vect2d that is this one scaled by a scalar value.
210.         about: Individually multiplies the x and y components by a single value.
211.         EndRem
212.         Method scale:vect2d(v!)
213.                 Return Create(x*v,y*v)
214.         End Method
215.         Rem
216.         bbdoc: Scales this vect2d by a scalar value.
217.         about: Individually multiplies the x and y components by a single value.
218.         EndRem
219.         Method applyscale(v!)
220.                 x:*v;y:*v
221.         End Method
222.         Rem
223.         bbdoc: Returns the dot product of this and another vect2d.
224.         EndRem
225.         Method dot!(o:vect2d)
226.                 Return x*o.x+y*o.y
227.         End Method
228.         Rem
229.         bbdoc: Returns the magnitude of the z component of the cross product of this and another vect2d.
230.         EndRem
231.         Method cross!(o:vect2d)
232.                 Return y*o.y-x*o.x
233.         End Method
234.         Rem
235.         bbdoc: Returns the magnitude of this vect2d.
236.         about: The length or magnitude of a vector is its distance from the origin.
237.         EndRem
238.         Method length!()
239.                 Return Sqr((x*x)+(y*y))
240.         End Method
241.         Rem
242.         bbdoc: Returns the magnitude of this vect2d squared. (Faster than length() due to no Sqr() call and perfectly adequate for comparisons.)
243.         about: The length or magnitude of a vector is its distance from the origin.
244.         EndRem
245.         Method lengthsq!()
246.                 Return ((x*x)+(y*y))
247.         End Method
248.         Rem
249.         bbdoc: Returns the distance from this vect2d to another.
250.         EndRem
251.         Method dist!(o:vect2d)
252.                 Local dx!=x-o.x
253.                 Local dy!=y-o.y
254.                 Return Sqr((dx*dx)+(dy*dy))
255.         End Method
256.         Rem
257.         bbdoc: Returns the distance from this vect2d to another squared. (Faster than length() due to no Sqr() call and perfectly adequate for comparisons.)
258.         EndRem
259.         Method distsq!(o:vect2d)
260.                 Local dx!=x-o.x
261.                 Local dy!=y-o.y
262.                 Return ((dx*dx)+(dy*dy))
263.         End Method
264.         Rem
265.         bbdoc: Returns the normalization of this vect2d.
266.         about: The normalization of a vector is when its components are divided by its magnitude so that it adopts a unit length.
267.         EndRem
268.         Method normalize:vect2d()
269.                 Local v!=length()
270.                 Return Create(x/v,y/v)
271.         End Method
272.         Rem
273.         bbdoc: Causes this vect2d to become its normalization.
274.         about: The normalization of a vector is when its components are divided by its magnitude so that it adopts a unit length.
275.         EndRem
276.         Method applynormalize()
277.                 Local v!=length()
278.                 x:/v;y:/v
279.         End Method
280.         Rem
281.         bbdoc: Returns the normalization of this vect2d.
282.         about: Protects against diviside by zero errors.
283.         EndRem
284.         Method normalizesafe:vect2d()
285.                 Local v!=length()
286.                 If v=0 Return New vect2d
287.                 Return Create(x/v,y/v)
288.         End Method
289.         Rem
290.         bbdoc: Causes this vect2d to become its normalization.
291.         about:  Protects against diviside by zero errors.
292.         EndRem
293.         Method applynormalizesafe()
294.                 Local v!=length()
295.                 If v=0 x=0;y=0;Return
296.                 x:/v;y:/v
297.         End Method
298.         Rem
299.         bbdoc: Returns a vect2d that is this one clamped to the given length.
300.         EndRem
301.         Method clamp:vect2d(length!)
302.                 If lengthsq()>length*length
303.                         Return normalize().scale(length)
304.                 Else
305.                         Return copy()
306.                 EndIf
307.         End Method
308.         Rem
309.         bbdoc: Clamp this vect2d to the given length.
310.         EndRem
311.         Method applyclamp(length!)
312.                 If lengthsq()>length*length
313.                         applynormalize()
314.                         applyscale(length)
315.                 EndIf
316.         End Method
317.         Rem
318.         bbdoc: Returns a perpendicular vect2d. (90 degree rotation)
319.         EndRem
320.         Method perp:vect2d()
321.                 Return Create(-y,x)
322.         End Method
323.         Rem
324.         bbdoc: Returns a perpendicular vect2d. (-90 degree rotation)
325.         EndRem
326.         Method rperp:vect2d()
327.                 Return Create(y,-x)
328.         End Method
329.         Rem
330.         bbdoc: Returns the vector projection of this onto o.
331.         EndRem
332.         Method project:vect2d(o:vect2d)
333.                 Return o.scale(dot(o)/o.lengthsq())
334.         End Method
335.         Rem
336.         bbdoc: Uses complex number multiplication to rotate this vect2d by another.
337.         about: Scaling will occur if this is not a unit vector.
338.         EndRem
339.         Method rotate:vect2d(o:vect2d)
340.                 Return Create(x*o.x-y*o.y,x*o.y+y*o.x)
341.         End Method
342.         Rem
343.         bbdoc: Inverse of rotate()
344.         EndRem
345.         Method unrotate:vect2d(o:vect2d)
346.                 Return Create(x*o.x+y*o.y,y*o.x-x*o.y)
347.         End Method
348.         Rem
349.         bbdoc: Linearly interpolate between this vect2d and another.
350.         EndRem
351.         Method lerp:vect2d(o:vect2d,t!)
353.         End Method
354.         Rem
355.         bbdoc: Linearly interpolate between this vect2d and another by the given distance.
356.         EndRem
357.         Method lerpconst:vect2d(o:vect2d,distance#)
359.         End Method
360.         Rem
361.         bbdoc: Returns the angular direction from this vect2d to another. (In degrees)
362.         EndRem
363.         Method angleto!(o:vect2d)
364.                 Return ATan2(o.y-y,o.x-x)
365.         End Method
366.         Rem
367.         bbdoc: Returns the unit length vect2d for the given angle. (In degrees)
368.         EndRem
369.         Function forangle:vect2d(angle!)
370.                 Return Create(Cos(angle),Sin(angle))
371.         End Function
372.         Rem
373.         bbdoc: Returns the angular direction this vect2d is pointing in. (In degrees)
374.         EndRem
375.         Method toangle!()
376.                 Return ATan2(y,x)
377.         End Method
378.         Rem
379.         bbdoc: Returns a string representation of this vect2d.
380.         EndRem
381.         Method ToString\$()
382.                 Return x+","+y
383.         End Method
384.         Rem
385.         bbdoc: Compare two vect2ds by their length.
386.         EndRem
387.         Method compare%(o2:Object)
388.                 Return comparevects(vect2d(o2))
389.         End Method
390. End Type

`x*o.y-y*o.x`