BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

polygon manipulation part 2

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Graphics
View previous topic :: View next topic  
Author Message
Jon Lennart Aasenden
Guest





PostPosted: Mon Aug 08, 2005 4:10 pm    Post subject: polygon manipulation part 2 Reply with quote



Hello again.

I guess i was not that good at explaining my problem.
I believe it is a simple task, perhaps to simple, for those of you that
replied.
I know Nils has constructed a whole polygon dtp package, so my humble
requirements became perhaps a bit blown out of proportions.

ok, here goes:

ROTATION
=========
Let's say i have a polygon of a 5 pointed star, pointing upwards like a
sherrif's badge.
I would like to rotate the points that make up this star, so (for example)
it points to the right.. or indeed, any degree between 0..359

For example:

type
TAngleRange = 0..359;

(* rotate around itself *)
Procedure RotatePoly(var APoints:Array of TPoint;Const Angle:TAngleRange);

(* rotate around a external fixed point *)
Procedure Procedure RotatePolyEx(var APoints:Array of
TPoint;Origo:TPoint;Const Angle:TAngleRange);

RotatePoly could probably call RotatePolyEx with a center point from the
original polygon.
Does anyone have somthing like this around that does not need a whole
support unit?

SCALING
========

Again, let's assume i have a 5 pointed star as a polygon.
It exists within a rectangle of 10,10 .. 110,110.
I would like to inflate it by say, 6 pixels, so it spans from 4,4..116,116
or deflate it :)

perhaps somthing like:

Procedure InflateRect(var APoints:Array of TPoint;Const Pixels:Word);

One of those that replied mentioned somthing about placing the origo at
x0/y0?
That would mean that the above would look exist within a rectangle of
-50,-50 .. 50,50 right?

While i understand the logic of it, (left & top is negative, right bottom is
positive) i need an example to see the matrix stuff in action. Could anyone
help me out? It would mean a lot.
Math was never my strong side im afraid, i have a learning problem there.

I will ofcourse check out TPolygon32 as a reference and try my best to
understand it.
I seem to remember that Nils had some sort of tutorial for this online?

Kind regards

Jon Lennart Aasenden


Back to top
Alfred ten Hoeve
Guest





PostPosted: Tue Aug 09, 2005 1:42 pm    Post subject: Re: polygon manipulation part 2 Reply with quote



Here is a small unit you can use.

unit Transformations;

interface

uses windows;

type
PointArray = array of TPoint;

TPunt = record // something like TPoint, but now for reals
r : extended;
phi : extended
end;

function Zwaartepunt(InPoints : array of TPoint): TPoint; // Center of
gravity
function Multiply(factor : extended; InPoints : array of TPoint; Center :
TPoint) : PointArray; // scaling
function Rotate(angle : extended; InPoints : array of TPoint; Center :
TPoint) : PointArray;

implementation

uses Math;

function Zwaartepunt(InPoints : array of TPoint): TPoint;
var
i, num : integer;
begin
num := High(InPoints);
Result.X := 0;
Result.Y := 0;
for i := 0 to num do
begin
Result.X := Result.X + InPoints[i].X;
Result.Y := Result.Y + InPoints[i].Y;
end;
Result.X := Round(Result.X / (num+1));
Result.Y := Round(Result.Y / (num+1));
end;

function Multiply(factor : extended; InPoints : array of TPoint; Center :
TPoint) : PointArray;
var
i, num : integer;
Q : array of TPunt;
begin
num := High(InPoints);
SetLength(Result, num + 1);
SetLength(Q, num + 1);
for i := 0 to num do
begin
Q[i].r := factor * sqrt(sqr(InPoints[i].X - Center.X) +
sqr(InPoints[i].Y - Center.Y));
Q[i].phi := arcTan2(InPoints[i].Y - Center.Y, InPoints[i].X - Center.X);

Result[i].X := Round(Center.X + Q[i].r * cos(Q[i].phi));
Result[i].Y := Round(Center.Y + Q[i].r * sin(Q[i].phi));
end;
end;

function Rotate(angle : extended; InPoints : array of TPoint; Center :
TPoint) : PointArray;
var
i, num : integer;
Q : array of TPunt;
begin
num := High(InPoints);
SetLength(Result, num + 1);
SetLength(Q, num + 1);
for i := 0 to num do
begin
Q[i].r := sqrt(sqr(InPoints[i].X - Center.X) + sqr(InPoints[i].Y -
Center.Y));
Q[i].phi := angle + arcTan2(InPoints[i].Y - Center.Y, InPoints[i].X -
Center.X);

Result[i].X := Round(Center.X + Q[i].r * cos(Q[i].phi));
Result[i].Y := Round(Center.Y + Q[i].r * sin(Q[i].phi));
end;
end;

end.

You need to draw the figures yourself.

Hope this helps.
Alfred.


Back to top
Jon Lennart Aasenden
Guest





PostPosted: Tue Aug 09, 2005 3:23 pm    Post subject: Re: polygon manipulation part 2 Reply with quote



Looks like just the thing i was looking for!

Thanks for the help!

Jon Lennart Aasenden

"Alfred ten Hoeve" <Alfred.nospam (AT) nospam (DOT) nl> wrote

Quote:
Here is a small unit you can use.

unit Transformations;

interface

uses windows;

type
PointArray = array of TPoint;

TPunt = record // something like TPoint, but now for reals
r : extended;
phi : extended
end;

function Zwaartepunt(InPoints : array of TPoint): TPoint; // Center of
gravity
function Multiply(factor : extended; InPoints : array of TPoint; Center :
TPoint) : PointArray; // scaling
function Rotate(angle : extended; InPoints : array of TPoint; Center :
TPoint) : PointArray;

implementation

uses Math;

function Zwaartepunt(InPoints : array of TPoint): TPoint;
var
i, num : integer;
begin
num := High(InPoints);
Result.X := 0;
Result.Y := 0;
for i := 0 to num do
begin
Result.X := Result.X + InPoints[i].X;
Result.Y := Result.Y + InPoints[i].Y;
end;
Result.X := Round(Result.X / (num+1));
Result.Y := Round(Result.Y / (num+1));
end;

function Multiply(factor : extended; InPoints : array of TPoint; Center :
TPoint) : PointArray;
var
i, num : integer;
Q : array of TPunt;
begin
num := High(InPoints);
SetLength(Result, num + 1);
SetLength(Q, num + 1);
for i := 0 to num do
begin
Q[i].r := factor * sqrt(sqr(InPoints[i].X - Center.X) +
sqr(InPoints[i].Y - Center.Y));
Q[i].phi := arcTan2(InPoints[i].Y - Center.Y, InPoints[i].X -
Center.X);

Result[i].X := Round(Center.X + Q[i].r * cos(Q[i].phi));
Result[i].Y := Round(Center.Y + Q[i].r * sin(Q[i].phi));
end;
end;

function Rotate(angle : extended; InPoints : array of TPoint; Center :
TPoint) : PointArray;
var
i, num : integer;
Q : array of TPunt;
begin
num := High(InPoints);
SetLength(Result, num + 1);
SetLength(Q, num + 1);
for i := 0 to num do
begin
Q[i].r := sqrt(sqr(InPoints[i].X - Center.X) + sqr(InPoints[i].Y -
Center.Y));
Q[i].phi := angle + arcTan2(InPoints[i].Y - Center.Y, InPoints[i].X -
Center.X);

Result[i].X := Round(Center.X + Q[i].r * cos(Q[i].phi));
Result[i].Y := Round(Center.Y + Q[i].r * sin(Q[i].phi));
end;
end;

end.

You need to draw the figures yourself.

Hope this helps.
Alfred.





Back to top
John Herbster
Guest





PostPosted: Tue Aug 09, 2005 10:09 pm    Post subject: Re: polygon manipulation part 2 Reply with quote


"Jon Lennart Aasenden" <post_nospam_nojunk (AT) jurasoft (DOT) no> wrote
Quote:
I guess I was not that good at explaining my problem.
ROTATION
=========
Let's say I have a polygon ... I would like to rotate the points ...
to the right ... any degree between 0 .. 359. For example:
type TAngleRange = 0 .. 359;
(* rotate around itself *)
Procedure RotatePoly(var APoints:Array of TPoint;Const
Angle:TAngleRange);


Jon,
Why are you trying to do this in integer, when it would be so simple
to do it in floating point? If it is for speed, integer is not much
faster.
Rgds, JohnH


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Graphics All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.