1 module vector_2d;
2 
3 import Math = math;
4 
5 import vector_2i;
6 import matrix_2d;
7 import matrix_3x2d;
8 
9 /*
10  * The MIT License
11  *
12  * Copyright (c) 2015-2021 Richard Greenlees
13  $@#!$@# Translated by jordan4ibanez
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a copy
16  * of this software and associated documentation files (the "Software"), to deal
17  * in the Software without restriction, including without limitation the rights
18  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19  * copies of the Software, and to permit persons to whom the Software is
20  * furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31  * THE SOFTWARE.
32  */
33 
34 /**
35  * Represents a 2D vector with double-precision.
36  *
37  * @author RGreenlees
38  * @author Kai Burjack
39  * @author F. Neurath
40  */
41 struct Vector2d {
42 
43 
44     /**
45      * The x component of the vector.
46      */
47     double x = 0.0;
48     /**
49      * The y component of the vector.
50      */
51     double y = 0.0;
52 
53     /**
54      * Create a new {@link Vector2d} and initialize both of its components with the given value.
55      * 
56      * @param d    
57      *          the value of both components
58      */
59     this(double d) {
60         this.x = d;
61         this.y = d;
62     }
63 
64     /**
65      * Create a new {@link Vector2d} and initialize its components to the given values.
66      * 
67      * @param x
68      *          the x value
69      * @param y
70      *          the y value
71      */
72     this(double x, double y) {
73         this.x = x;
74         this.y = y;
75     }
76 
77     /**
78      * Create a new {@link Vector2d} and initialize its components to the one of the given vector.
79      * 
80      * @param v
81      *          the {@link Vector2d} to copy the values from
82      */
83     this(Vector2d v) {
84         x = v.x;
85         y = v.y;
86     }
87 
88     /**
89      * Create a new {@link Vector2d} and initialize its components to the one of the given vector.
90      * 
91      * @param v
92      *          the {@link Vector2i} to copy the values from
93      */
94     this(Vector2i v) {
95         x = v.x;
96         y = v.y;
97     }
98 
99     /**
100      * Create a new {@link Vector2d} and initialize its two components from the first
101      * two elements of the given array.
102      * 
103      * @param xy
104      *          the array containing at least three elements
105      */
106     this(double[] xy) {
107         this.x = xy[0];
108         this.y = xy[1];
109     }
110 
111     /**
112      * Set the x and y components to the supplied value.
113      *
114      * @param d
115      *          the value of both components
116      * @return this
117      */
118     ref public Vector2d set(double d) return {
119         this.x = d;
120         this.y = d;
121         return this;
122     }
123 
124     /**
125      * Set the x and y components to the supplied values.
126      * 
127      * @param x
128      *          the x value
129      * @param y
130      *          the y value
131      * @return this
132      */
133     ref public Vector2d set(double x, double y) return {
134         this.x = x;
135         this.y = y;
136         return this;
137     }
138 
139     /**
140      * Set this {@link Vector2d} to the values of v.
141      * 
142      * @param v
143      *          the vector to copy from
144      * @return this
145      */
146     ref public Vector2d set(Vector2d v) return {
147         this.x = v.x;
148         this.y = v.y;
149         return this;
150     }
151 
152 
153     /**
154      * Set this {@link Vector2d} to be a clone of <code>v</code>.
155      * 
156      * @param v
157      *          the vector to copy from
158      * @return this
159      */
160     ref public Vector2d set(Vector2i v) return {
161         this.x = v.x;
162         this.y = v.y;
163         return this;
164     }
165 
166     /**
167      * Set the two components of this vector to the first two elements of the given array.
168      * 
169      * @param xy
170      *          the array containing at least three elements
171      * @return this
172      */
173     ref public Vector2d set(double[] xy) return {
174         this.x = xy[0];
175         this.y = xy[1];
176         return this;
177     }
178 
179     /**
180      * Set the two components of this vector to the first two elements of the given array.
181      * 
182      * @param xy
183      *          the array containing at least two elements
184      * @return this
185      */
186     ref public Vector2d set(float[] xy) return {
187         this.x = xy[0];
188         this.y = xy[1];
189         return this;
190     }
191 
192     public double get(int component) {
193         switch (component) {
194         case 0:
195             return x;
196         case 1:
197             return y;
198         default: 
199             return 0; // do nothing
200         }
201     }
202 
203     public Vector2i get(int mode, ref Vector2i dest) {
204         dest.x = Math.roundUsing(this.x, mode);
205         dest.y = Math.roundUsing(this.y, mode);
206         return dest;
207     }
208 
209     public Vector2d get(ref Vector2d dest) {
210         dest.x = this.x;
211         dest.y = this.y;
212         return dest;
213     }
214 
215     /**
216      * Set the value of the specified component of this vector.
217      *
218      * @param component
219      *          the component whose value to set, within <code>[0..1]</code>
220      * @param value
221      *          the value to set
222      * @return this
223      * @throws IllegalArgumentException if <code>component</code> is not within <code>[0..1]</code>
224      */
225     ref public Vector2d setComponent(int component, double value) return {
226         switch (component) {
227             case 0:
228                 x = value;
229                 break;
230             case 1:
231                 y = value;
232                 break;
233             default: {}
234         }
235         return this;
236     }
237 
238     /**
239      * Set this vector to be one of its perpendicular vectors.
240      * 
241      * @return this
242      */
243     ref public Vector2d perpendicular() return {
244         double xTemp = y;
245         this.y = x * -1;
246         this.x = xTemp;
247         return this;
248     }
249 
250     /**
251      * Subtract <code>v</code> from this vector.
252      * 
253      * @param v
254      *          the vector to subtract
255      * @return this
256      */
257     ref public Vector2d sub(Vector2d v) return {
258         this.x = x - v.x;
259         this.y = y - v.y;
260         return this;
261     }
262 
263     /**
264      * Subtract <code>(x, y)</code> from this vector.
265      * 
266      * @param x
267      *          the x component to subtract
268      * @param y
269      *          the y component to subtract
270      * @return this
271      */
272     ref public Vector2d sub(double x, double y) return {
273         this.x = this.x - x;
274         this.y = this.y - y;
275         return this;
276     }
277 
278     public Vector2d sub(double x, double y, ref Vector2d dest) {
279         dest.x = this.x - x;
280         dest.y = this.y - y;
281         return dest;
282     }
283 
284 
285     public Vector2d sub(Vector2d v, ref Vector2d dest) {
286         dest.x = x - v.x;
287         dest.y = y - v.y;
288         return dest;
289     }
290 
291 
292     /**
293      * Multiply the components of this vector by the given scalar.
294      * 
295      * @param scalar
296      *        the value to multiply this vector's components by
297      * @return this
298      */
299     ref public Vector2d mul(double scalar) return {
300         this.x = x * scalar;
301         this.y = y * scalar;
302         return this;
303     }
304 
305     public Vector2d mul(double scalar, ref Vector2d dest) {
306         dest.x = x * scalar;
307         dest.y = y * scalar;
308         return dest;
309     }
310 
311     /**
312      * Multiply the components of this Vector2d by the given scalar values and store the result in <code>this</code>.
313      * 
314      * @param x
315      *          the x component to multiply this vector by
316      * @param y
317      *          the y component to multiply this vector by
318      * @return this
319      */
320     ref public Vector2d mul(double x, double y) return {
321         this.x = this.x * x;
322         this.y = this.y * y;
323         return this;
324     }
325 
326     public Vector2d mul(double x, double y, ref Vector2d dest) {
327         dest.x = this.x * x;
328         dest.y = this.y * y;
329         return dest;
330     }
331 
332     /**
333      * Multiply this Vector2d component-wise by another Vector2d.
334      * 
335      * @param v
336      *          the vector to multiply by
337      * @return this
338      */
339     ref public Vector2d mul(Vector2d v) return {
340         this.x = x * v.x;
341         this.y = y * v.y;
342         return this;
343     }
344 
345     public Vector2d mul(Vector2d v, ref Vector2d dest) {
346         dest.x = x * v.x;
347         dest.y = y * v.y;
348         return dest;
349     }
350 
351     /**
352      * Divide this Vector2d by the given scalar value.
353      * 
354      * @param scalar
355      *          the scalar to divide this vector by
356      * @return this
357      */
358     ref public Vector2d div(double scalar) return {
359         double inv = 1.0 / scalar;
360         this.x = x * inv;
361         this.y = y * inv;
362         return this;
363     }
364 
365     public Vector2d div(double scalar, ref Vector2d dest) {
366         double inv = 1.0 / scalar;
367         dest.x = x * inv;
368         dest.y = y * inv;
369         return dest;
370     }
371 
372     /**
373      * Divide the components of this Vector2d by the given scalar values and store the result in <code>this</code>.
374      * 
375      * @param x
376      *          the x component to divide this vector by
377      * @param y
378      *          the y component to divide this vector by
379      * @return this
380      */
381     ref public Vector2d div(double x, double y) return {
382         this.x = this.x / x;
383         this.y = this.y / y;
384         return this;
385     }
386 
387     public Vector2d div(double x, double y, ref Vector2d dest) {
388         dest.x = this.x / x;
389         dest.y = this.y / y;
390         return dest;
391     }
392 
393     /**
394      * Divide this Vector2d component-wise by another Vector2d.
395      * 
396      * @param v
397      *          the vector to divide by
398      * @return this
399      */
400     ref public Vector2d div(Vector2d v) return {
401         this.x = x / v.x;
402         this.y = y / v.y;
403         return this;
404     }
405 
406 
407     public Vector2d div(Vector2d v, ref Vector2d dest) {
408         dest.x = x / v.x;
409         dest.y = y / v.y;
410         return dest;
411     }
412 
413     /**
414      * Multiply the given matrix <code>mat</code> with this Vector2d.
415      *
416      * @param mat
417      *          the matrix to multiply this vector by
418      * @return this
419      */
420     ref public Vector2d mul(Matrix2d mat) return {
421         double rx = mat.m00 * x + mat.m10 * y;
422         double ry = mat.m01 * x + mat.m11 * y;
423         this.x = rx;
424         this.y = ry;
425         return this;
426     }
427 
428     public Vector2d mul(Matrix2d mat, ref Vector2d dest) {
429         double rx = mat.m00 * x + mat.m10 * y;
430         double ry = mat.m01 * x + mat.m11 * y;
431         dest.x = rx;
432         dest.y = ry;
433         return dest;
434     }
435     /**
436      * Multiply the transpose of the given matrix with this Vector2d and store the result in <code>this</code>.
437      *
438      * @param mat
439      *          the matrix
440      * @return this
441      */
442     ref public Vector2d mulTranspose(Matrix2d mat) return {
443         double rx = mat.m00 * x + mat.m01 * y;
444         double ry = mat.m10 * x + mat.m11 * y;
445         this.x = rx;
446         this.y = ry;
447         return this;
448     }
449 
450     public Vector2d mulTranspose(Matrix2d mat, ref Vector2d dest) {
451         double rx = mat.m00 * x + mat.m01 * y;
452         double ry = mat.m10 * x + mat.m11 * y;
453         dest.x = rx;
454         dest.y = ry;
455         return dest;
456     }
457 
458     /**
459      * Multiply the given 3x2 matrix <code>mat</code> with <code>this</code>.
460      * <p>
461      * This method assumes the <code>z</code> component of <code>this</code> to be <code>1.0</code>.
462      * 
463      * @param mat
464      *          the matrix to multiply this vector by
465      * @return this
466      */
467     ref public Vector2d mulPosition(Matrix3x2d mat) return {
468         double rx = mat.m00 * x + mat.m10 * y + mat.m20;
469         double ry = mat.m01 * x + mat.m11 * y + mat.m21;
470         this.x = rx;
471         this.y = ry;
472         return this;
473     }
474 
475     public Vector2d mulPosition(Matrix3x2d mat, ref Vector2d dest) {
476         double rx = mat.m00 * x + mat.m10 * y + mat.m20;
477         double ry = mat.m01 * x + mat.m11 * y + mat.m21;
478         dest.x = rx;
479         dest.y = ry;
480         return dest;
481     }
482 
483     /**
484      * Multiply the given 3x2 matrix <code>mat</code> with <code>this</code>.
485      * <p>
486      * This method assumes the <code>z</code> component of <code>this</code> to be <code>0.0</code>.
487      * 
488      * @param mat
489      *          the matrix to multiply this vector by
490      * @return this
491      */
492     ref public Vector2d mulDirection(Matrix3x2d mat) return {
493         double rx = mat.m00 * x + mat.m10 * y;
494         double ry = mat.m01 * x + mat.m11 * y;
495         this.x = rx;
496         this.y = ry;
497         return this;
498     }
499 
500     public Vector2d mulDirection(Matrix3x2d mat, ref Vector2d dest) {
501         double rx = mat.m00 * x + mat.m10 * y;
502         double ry = mat.m01 * x + mat.m11 * y;
503         dest.x = rx;
504         dest.y = ry;
505         return dest;
506     }
507 
508     public double dot(Vector2d v) {
509         return x * v.x + y * v.y;
510     }
511 
512     public double angle(Vector2d v) {
513         double dot = x*v.x + y*v.y;
514         double det = x*v.y - y*v.x;
515         return Math.atan2(det, dot);
516     }
517 
518     public double lengthSquared() {
519         return x * x + y * y;
520     }
521 
522     /**
523      * Get the length squared of a 2-dimensional double-precision vector.
524      *
525      * @param x The vector's x component
526      * @param y The vector's y component
527      *
528      * @return the length squared of the given vector
529      *
530      * @author F. Neurath
531      */
532     public static double lengthSquared(double x, double y) {
533         return x * x + y * y;
534     }
535 
536     public double length() {
537         return Math.sqrt(x * x + y * y);
538     }
539 
540     /**
541      * Get the length of a 2-dimensional double-precision vector.
542      *
543      * @param x The vector's x component
544      * @param y The vector's y component
545      *
546      * @return the length of the given vector
547      *
548      * @author F. Neurath
549      */
550     public static double length(double x, double y) {
551         return Math.sqrt(x * x + y * y);
552     }
553 
554     public double distance(Vector2d v) {
555         double dx = this.x - v.x;
556         double dy = this.y - v.y;
557         return Math.sqrt(dx * dx + dy * dy);
558     }
559 
560     public double distanceSquared(Vector2d v) {
561         double dx = this.x - v.x;
562         double dy = this.y - v.y;
563         return dx * dx + dy * dy;
564     }
565 
566     public double distance(double x, double y) {
567         double dx = this.x - x;
568         double dy = this.y - y;
569         return Math.sqrt(dx * dx + dy * dy);
570     }
571 
572     public double distanceSquared(double x, double y) {
573         double dx = this.x - x;
574         double dy = this.y - y;
575         return dx * dx + dy * dy;
576     }
577 
578     /**
579      * Return the distance between <code>(x1, y1)</code> and <code>(x2, y2)</code>.
580      *
581      * @param x1
582      *          the x component of the first vector
583      * @param y1
584      *          the y component of the first vector
585      * @param x2
586      *          the x component of the second vector
587      * @param y2
588      *          the y component of the second vector
589      * @return the euclidean distance
590      */
591     public static double distance(double x1, double y1, double x2, double y2) {
592         double dx = x1 - x2;
593         double dy = y1 - y2;
594         return Math.sqrt(dx * dx + dy * dy);
595     }
596 
597     /**
598      * Return the squared distance between <code>(x1, y1)</code> and <code>(x2, y2)</code>.
599      *
600      * @param x1
601      *          the x component of the first vector
602      * @param y1
603      *          the y component of the first vector
604      * @param x2
605      *          the x component of the second vector
606      * @param y2
607      *          the y component of the second vector
608      * @return the euclidean distance squared
609      */
610     public static double distanceSquared(double x1, double y1, double x2, double y2) {
611         double dx = x1 - x2;
612         double dy = y1 - y2;
613         return dx * dx + dy * dy;
614     }
615 
616     /**
617      * Normalize this vector.
618      * 
619      * @return this
620      */
621     ref public Vector2d normalize() return {
622         double invLength = Math.invsqrt(x * x + y * y);
623         this.x = x * invLength;
624         this.y = y * invLength;
625         return this;
626     }
627 
628     public Vector2d normalize(ref Vector2d dest) {
629         double invLength = Math.invsqrt(x * x + y * y);
630         dest.x = x * invLength;
631         dest.y = y * invLength;
632         return dest;
633     }
634 
635     /**
636      * Scale this vector to have the given length.
637      * 
638      * @param length
639      *          the desired length
640      * @return this
641      */
642     ref public Vector2d normalize(double length) return {
643         double invLength = Math.invsqrt(x * x + y * y) * length;
644         this.x = x * invLength;
645         this.y = y * invLength;
646         return this;
647     }
648 
649     public Vector2d normalize(double length, ref Vector2d dest) {
650         double invLength = Math.invsqrt(x * x + y * y) * length;
651         dest.x = x * invLength;
652         dest.y = y * invLength;
653         return dest;
654     }
655 
656     /**
657      * Add <code>v</code> to this vector.
658      * 
659      * @param v
660      *          the vector to add
661      * @return this
662      */
663     ref public Vector2d add(Vector2d v) return {
664         this.x = x + v.x;
665         this.y = y + v.y;
666         return this;
667     }
668 
669     /**
670      * Add <code>(x, y)</code> to this vector.
671      * 
672      * @param x
673      *          the x component to add
674      * @param y
675      *          the y component to add
676      * @return this
677      */
678     ref public Vector2d add(double x, double y) return {
679         this.x = this.x + x;
680         this.y = this.y + y;
681         return this;
682     }
683 
684     public Vector2d add(double x, double y, ref Vector2d dest) {
685         dest.x = this.x + x;
686         dest.y = this.y + y;
687         return dest;
688     }
689 
690 
691     public Vector2d add(Vector2d v, ref Vector2d dest) {
692         dest.x = x + v.x;
693         dest.y = y + v.y;
694         return dest;
695     }
696 
697     /**
698      * Set all components to zero.
699      * 
700      * @return this
701      */
702     ref public Vector2d zero() return {
703         this.x = 0;
704         this.y = 0;
705         return this;
706     }
707 
708     /**
709      * Negate this vector.
710      * 
711      * @return this
712      */
713     ref public Vector2d negate() return {
714         this.x = -x;
715         this.y = -y;
716         return this;
717     }
718 
719     public Vector2d negate(ref Vector2d dest) {
720         dest.x = -x;
721         dest.y = -y;
722         return dest;
723     }
724 
725     /**
726      * Linearly interpolate <code>this</code> and <code>other</code> using the given interpolation factor <code>t</code>
727      * and store the result in <code>this</code>.
728      * <p>
729      * If <code>t</code> is <code>0.0</code> then the result is <code>this</code>. If the interpolation factor is <code>1.0</code>
730      * then the result is <code>other</code>.
731      * 
732      * @param other
733      *          the other vector
734      * @param t
735      *          the interpolation factor between 0.0 and 1.0
736      * @return this
737      */
738     ref public Vector2d lerp(Vector2d other, double t) return {
739         this.x = x + (other.x - x) * t;
740         this.y = y + (other.y - y) * t;
741         return this;
742     }
743 
744     public Vector2d lerp(Vector2d other, double t, ref Vector2d dest) {
745         dest.x = x + (other.x - x) * t;
746         dest.y = y + (other.y - y) * t;
747         return dest;
748     }
749 
750     public int hashCode() {
751         immutable int prime = 31;
752         int result = 1;
753         long temp;
754         temp = Math.doubleToLongBits(x);
755         result = prime * result + cast(int) (temp ^ (temp >>> 32));
756         temp = Math.doubleToLongBits(y);
757         result = prime * result + cast(int) (temp ^ (temp >>> 32));
758         return result;
759     }
760 
761     public bool equals(Vector2d v, double delta) {
762         if (this == v)
763             return true;
764         if (!Math.equals(x, v.x, delta))
765             return false;
766         if (!Math.equals(y, v.y, delta))
767             return false;
768         return true;
769     }
770 
771     public bool equals(double x, double y) {
772         if (Math.doubleToLongBits(this.x) != Math.doubleToLongBits(x))
773             return false;
774         if (Math.doubleToLongBits(this.y) != Math.doubleToLongBits(y))
775             return false;
776         return true;
777     }
778 
779     /**
780      * Add the component-wise multiplication of <code>a * b</code> to this vector.
781      * 
782      * @param a
783      *          the first multiplicand
784      * @param b
785      *          the second multiplicand
786      * @return this
787      */
788     ref public Vector2d fma(Vector2d a, Vector2d b) return {
789         this.x = x + a.x * b.x;
790         this.y = y + a.y * b.y;
791         return this;
792     }
793 
794     /**
795      * Add the component-wise multiplication of <code>a * b</code> to this vector.
796      * 
797      * @param a
798      *          the first multiplicand
799      * @param b
800      *          the second multiplicand
801      * @return this
802      */
803     ref public Vector2d fma(double a, Vector2d b) return {
804         this.x = x + a * b.x;
805         this.y = y + a * b.y;
806         return this;
807     }
808 
809     public Vector2d fma(Vector2d a, Vector2d b, ref Vector2d dest) {
810         dest.x = x + a.x * b.x;
811         dest.y = y + a.y * b.y;
812         return dest;
813     }
814 
815     public Vector2d fma(double a, Vector2d b, ref Vector2d dest) {
816         dest.x = x + a * b.x;
817         dest.y = y + a * b.y;
818         return dest;
819     }
820 
821     /**
822      * Set the components of this vector to be the component-wise minimum of this and the other vector.
823      *
824      * @param v
825      *          the other vector
826      * @return this
827      */
828     ref public Vector2d min(Vector2d v) return {
829         this.x = x < v.x ? x : v.x;
830         this.y = y < v.y ? y : v.y;
831         return this;
832     }
833 
834     public Vector2d min(Vector2d v, ref Vector2d dest) {
835         dest.x = x < v.x ? x : v.x;
836         dest.y = y < v.y ? y : v.y;
837         return dest;
838     }
839 
840     /**
841      * Set the components of this vector to be the component-wise maximum of this and the other vector.
842      *
843      * @param v
844      *          the other vector
845      * @return this
846      */
847     ref public Vector2d max(Vector2d v) return {
848         this.x = x > v.x ? x : v.x;
849         this.y = y > v.y ? y : v.y;
850         return this;
851     }
852 
853     public Vector2d max(Vector2d v, ref Vector2d dest) {
854         dest.x = x > v.x ? x : v.x;
855         dest.y = y > v.y ? y : v.y;
856         return dest;
857     }
858 
859     public int maxComponent() {
860         double absX = Math.abs(x);
861         double absY = Math.abs(y);
862         if (absX >= absY)
863             return 0;
864         return 1;
865     }
866 
867     public int minComponent() {
868         double absX = Math.abs(x);
869         double absY = Math.abs(y);
870         if (absX < absY)
871             return 0;
872         return 1;
873     }
874 
875     /**
876      * Set each component of this vector to the largest (closest to positive
877      * infinity) {@code double} value that is less than or equal to that
878      * component and is equal to a mathematical integer.
879      *
880      * @return this
881      */
882     ref public Vector2d floor() return {
883         this.x = Math.floor(x);
884         this.y = Math.floor(y);
885         return this;
886     }
887 
888     public Vector2d floor(ref Vector2d dest) {
889         dest.x = Math.floor(x);
890         dest.y = Math.floor(y);
891         return dest;
892     }
893 
894     /**
895      * Set each component of this vector to the smallest (closest to negative
896      * infinity) {@code double} value that is greater than or equal to that
897      * component and is equal to a mathematical integer.
898      *
899      * @return this
900      */
901     ref public Vector2d ceil() return {
902         this.x = Math.ceil(x);
903         this.y = Math.ceil(y);
904         return this;
905     }
906 
907     public Vector2d ceil(ref Vector2d dest) {
908         dest.x = Math.ceil(x);
909         dest.y = Math.ceil(y);
910         return dest;
911     }
912 
913     /**
914      * Set each component of this vector to the closest double that is equal to
915      * a mathematical integer, with ties rounding to positive infinity.
916      *
917      * @return this
918      */
919     ref public Vector2d round() return {
920         this.x = Math.round(x);
921         this.y = Math.round(y);
922         return this;
923     }
924 
925     public Vector2d round(ref Vector2d dest) {
926         dest.x = Math.round(x);
927         dest.y = Math.round(y);
928         return dest;
929     }
930 
931     public bool isFinite() {
932         return Math.isFinite(x) && Math.isFinite(y);
933     }
934 
935     /**
936      * Set <code>this</code> vector's components to their respective absolute values.
937      * 
938      * @return this
939      */
940     ref public Vector2d absolute() return {
941         this.x = Math.abs(this.x);
942         this.y = Math.abs(this.y);
943         return this;
944     }
945 
946     public Vector2d absolute(ref Vector2d dest) {
947         dest.x = Math.abs(this.x);
948         dest.y = Math.abs(this.y);
949         return dest;
950     }
951 
952 }