Vector: Difference between revisions

950 bytes added ,  12 days ago
PascalABC.NET
m (Update Lang example: Use new struct definition syntax)
(PascalABC.NET)
Line 2,210:
y=x~'*'(3) => [-4.24264068,-4.24264068]
z=x~'/'(0.1) => [-14.1421356,-14.1421356]</pre>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
type
Vector = class
x,y: real;
public
constructor (xx,yy: real) := (x,y) := (xx,yy);
function ToString: string; override := $'({x},{y})';
end;
function operator+(v1,v2: Vector): Vector; extensionmethod
:= new Vector(v1.x + v2.x, v1.y + v2.y);
function operator-(v1,v2: Vector): Vector; extensionmethod
:= new Vector(v1.x - v2.x, v1.y - v2.y);
 
function operator*(v: Vector; n: real): Vector; extensionmethod
:= new Vector(v.x * n, v.y * n);
 
function operator*(n: real; v: Vector): Vector; extensionmethod
:= v * n;
 
function operator/(v: Vector; n: real): Vector; extensionmethod
:= new Vector(v.x / n, v.y / n);
 
begin
var v1 := new Vector(1,2);
var v2 := new Vector(3,4);
Println(v1 + v2);
Println(v1 - v2);
Println(v1 * 2.5, 2.5 * v1);
Println(v1 / 2);
end.
</syntaxhighlight>
{{out}}
<pre>
(4,6)
(-2,-2)
(2.5,5) (2.5,5)
(0.5,1)
</pre>
 
 
=={{header|Perl}}==
217

edits