Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Common / Geometry / Point.sc
blobadf31c91c5051e11dd71e4f43ed985de841d4780
1 Point {
2         var <>x = 0, <>y = 0;
4         *new { arg x=0, y=0;
5                 ^super.newCopyArgs(x, y);
6         }
7         @ { arg aPoint;
8                 ^Rect.fromPoints(this, aPoint)
9         }
11         set { arg argX=0, argY=0; x = argX; y = argY; }
13         asPoint { ^this }
14         asComplex { ^Complex.new(x,y) }
15         asPolar { ^Polar.new(this.rho, this.theta) }
16         asSize { ^Size(x,y) }
17         asRect { ^Rect.new(0,0,x,y) }
18         asArray { ^[this.x, this.y] }
20         == { arg aPoint;
21                 ^this.compareObject(aPoint, #[\x, \y])
22         }
23         hash {
24                 ^this.instVarHash(#[\x, \y])
25         }
26         performBinaryOpOnSomething { |aSelector, thing, adverb|
27                 ^thing.asPoint.perform(aSelector, this, adverb)
28         }
29         + { arg delta;
30                 var deltaPoint;
31                 deltaPoint = delta.asPoint;
32                 ^(this.x + deltaPoint.x) @ (this.y + deltaPoint.y)
33         }
34         - { arg delta;
35                 var deltaPoint;
36                 deltaPoint = delta.asPoint;
37                 ^(this.x - deltaPoint.x) @ (this.y - deltaPoint.y)
38         }
40         * { arg scale;
41                 var scalePoint;
42                 scalePoint = scale.asPoint;
43                 ^(this.x * scalePoint.x) @ (this.y * scalePoint.y)
44         }
45         / { arg scale;
46                 var scalePoint;
47                 scalePoint = scale.asPoint;
48                 ^(this.x / scalePoint.x) @ (this.y / scalePoint.y)
49         }
50         div { arg scale;
51                 var scalePoint;
52                 scalePoint = scale.asPoint;
53                 ^(this.x div: scalePoint.x) @ (this.y div: scalePoint.y)
54         }
55         translate { arg delta;
56                 ^(this.x + delta.x) @ (this.y + delta.y)
57         }
58         scale { arg scale;
59                 ^(this.x * scale.x) @ (this.y * scale.y)
60         }
61         rotate { arg angle; // in radians
62                 var sinr, cosr;
63                 sinr = angle.sin;
64                 cosr = angle.cos;
65                 ^((x * cosr) - (y * sinr)) @ ((y * cosr) + (x * sinr))
66         }
68         abs { ^x.abs @ y.abs }
70         rho { ^hypot(x, y) }
71         theta { ^atan2(y, x) }
73         dist { arg aPoint;
74                 aPoint = aPoint.asPoint;
75                 ^hypot(x - aPoint.x, y - aPoint.y)
76         }
77         transpose { ^y @ x }
79         round { arg quant;
80                 quant = quant.asPoint;
81                 ^x.round(quant.x) @ y.round(quant.y)
82         }
83         trunc { arg quant;
84                 quant = quant.asPoint;
85                 ^x.trunc(quant.x) @ y.trunc(quant.y)
86         }
88         mod {|that|
89                 var thatPoint;
90                 thatPoint = that.asPoint;
91                 ^(this.x mod: thatPoint.x) @ (this.y mod: thatPoint.y)
92         }
94         printOn { arg stream;
95                 stream << this.class.name << "( " << x << ", " << y << " )";
96         }
97         storeArgs { ^[x,y] }
101 PointArray : Point
103         *new { arg n;
104                 ^super.new(Signal.new(n), Signal.new(n))
105         }
106         add { arg point;
107                 x = x.add(point.x);
108                 y = y.add(point.y);
109         }
112 //Lines : PointArray
114 //      *new { arg n;
115 //              ^super.new(2*n)
116 //      }
119 //Polygon : PointArray
123 //ZigZag : PointArray