Smart Mobile Studio now supports helper objects for various elements, including classes, records and arrays. Helper’s are extremely valuable in leveraging complex data-structures. This is a very powerful addition to Smart’s already impressive list of “must have” features when working with javascript. Features that no existing javascript framework or utility provides.
Here is a small taste of how you can use a helper object to enrich something as simple as an array of TPoints. These are pure “trig” rotations (just one way of doing it).
[sourcecode language=”delphi”]
unit w3polygons;
interface
uses w3system;
type
TPolygonHelper = helper for TPointArray
Procedure Rotate(const angle:Float;const centerX,centerY:Float);
Procedure Scale(factor:float;Const centerX,centerY:Float);
End;
Implementation
procedure TPolygonHelper.Rotate(const angle:Float;
const centerX,centerY:Float);
var
i: integer;
r,p: Float;
begin
if self.Length>0 then
Begin
for i:=self.low to self.high do
begin
r:=sqrt(sqr(self[i].X – centerX) + sqr(self[i].Y – centerY));
p := angle + arcTan2(self[i].Y – centerY, self[i].X – centerX);
self[i].x:=Round(centerX + r * cos(p));
self[i].y:=Round(centerY + r * sin(p));
end;
end;
end;
Procedure TPolygonHelper.Scale(factor:Float;
Const centerX,centerY:Float);
var
i: integer;
r,p: float;
begin
if self.length>0 then
begin
for i := self.low to self.high do
begin
r:= factor * sqrt(sqr(self[i].X – centerX) + sqr(self[i].Y – centerY));
p:= arcTan2(self[i].Y – centerY, self[i].X – centerX);
self[i].X := Round(centerX + r * cos(p));
self[i].Y := Round(centerY + r * sin(p));
end;
end;
end;
end.
[/sourcecode]