Jump to content
opiumhautopium1

distance function ....question need your help

Recommended Posts

hi guys i got a question

is there a funktion to get a x/y coordinate

between the distance function ?

function Distance(const x1, y1, x2, y2: Integer): Integer;

example

x1=0... y1=0..... x2=10...... y2=0....... distance is 10

but now i want to know the (x/y) coordinate by 7 because i want to do a mouseclick betreen this 2 points (x1/y1) and (x2/y2) thx in advance and hope somebody can help me ^^

Link to comment
Share on other sites

give this funktion the center of 2 points? then it is not the right thing because i need the coordinates between 2 points but not the center/middle may be the distance between 2 points is 15 .... and i want to know the coordinate by distance step 9

 

or do i understand someting wrong with your function ?

Link to comment
Share on other sites

give this funktion the center of 2 points? then it is not the right thing because i need the coordinates between 2 points but not the center/middle may be the distance between 2 points is 15 .... and i want to know the coordinate by distance step 9

 

or do i understand someting wrong with your function ?

 

I dont really understand what you mean, but this function return the middle of a box so you can click in the middle of x1y1 and x2y2.

Link to comment
Share on other sites

Does this help you out (?):

 

[scar]function PointToStr(pt: TPoint): string;

begin

Result := (IntToStr(pt.X) + ', ' + IntToStr(pt.Y));

end;

 

function DistanceTo(fromPt, toPt: TPoint): Integer;

var

X, Y: Integer;

begin

if (fromPt = toPt) then

Exit;

X := (Max(fromPt.X, toPt.X) - Min(fromPt.X, toPt.X));

Y := (Max(fromPt.Y, toPt.Y) - Min(fromPt.Y, toPt.Y));

Result := Max(X, Y);

end;

 

function GetTPALine(fromPt, toPt: TPoint): TPointArray;

var

i, h: integer;

e: Extended;

begin

h := Max(Round(Abs(fromPt.X - toPt.X)), Round(Abs(fromPt.Y - toPt.Y)));

SetLength(Result, h + 1);

for i := 0 to h do

begin

e := (i / h);

Result.X := fromPt.X + Round((toPt.X - fromPt.X) * e);

Result.Y := fromPt.Y + Round((toPt.Y - fromPt.Y) * e);

end;

end;

 

function GetPointBetweenPointsByStep(fromPt, toPt: TPoint; step: Integer): TPoint;

var

i, h: integer;

e: Extended;

begin

if (step > DistanceTo(fromPt, toPt)) or (step < 0) then

Exit;

h := Max(Round(Abs(fromPt.X - toPt.X)), Round(Abs(fromPt.Y - toPt.Y)));

e := (step / h);

Result.X := fromPt.X + Round((toPt.X - fromPt.X) * e);

Result.Y := fromPt.Y + Round((toPt.Y - fromPt.Y) * e);

end;

 

var

TPA: TPointArray;

h, i: Integer;

pt1, pt2: TPoint;

 

begin

ClearDebug;

pt1 := Point(127, 106);

pt2 := Point(39, 34);

TPA := GetTPALine(pt1, pt2);

h := High(TPA);

for i := 0 to h do

begin

WriteLn('GetPointBetweenPointsByStep: ' + PointToStr(GetPointBetweenPointsByStep(pt1, pt2, i)) + ' (step: ' + IntToStr(i) + ')');

WriteLn('TPA[' + IntToStr(i) + ']: ' + PointToStr(TPA));

end;

SetLength(TPA, 0);

end.[/scar]

 

A few functions that I wrote. Pretty much does what you are looking for.. :) (Getting point from line of points, with "STEP" (chosen distance).

 

Edit: You can test it in Paint, with mouse clicking, that it does work correctly. :P

 

Edit2: Here is also example with your coordinates, that you were looking answer for..

 

[scar]function PointToStr(pt: TPoint): string;

begin

Result := (IntToStr(pt.X) + ', ' + IntToStr(pt.Y));

end;

 

function DistanceTo(fromPt, toPt: TPoint): Integer;

var

X, Y: Integer;

begin

if (fromPt = toPt) then

Exit;

X := (Max(fromPt.X, toPt.X) - Min(fromPt.X, toPt.X));

Y := (Max(fromPt.Y, toPt.Y) - Min(fromPt.Y, toPt.Y));

Result := Max(X, Y);

end;

 

function GetPointBetweenPointsByStep(fromPt, toPt: TPoint; step: Integer): TPoint;

var

i, h: integer;

e: Extended;

begin

if (step > DistanceTo(fromPt, toPt)) or (step < 0) then

Exit;

h := Max(Round(Abs(fromPt.X - toPt.X)), Round(Abs(fromPt.Y - toPt.Y)));

e := (step / h);

Result.X := fromPt.X + Round((toPt.X - fromPt.X) * e);

Result.Y := fromPt.Y + Round((toPt.Y - fromPt.Y) * e);

end;

 

var

pt1, pt2: TPoint;

 

begin

ClearDebug;

pt1 := Point(0, 0);

pt2 := Point(10, 0);

WriteLn(PointToStr(GetPointBetweenPointsByStep(pt1, pt2, 7)));

end.[/scar]

 

So thats how it works. :)

 

Edit3: GetTPABetweenPoints function, you might need this.. :)

 

[scar]function DistanceTo(fromPt, toPt: TPoint): Integer;

var

X, Y: Integer;

begin

if (fromPt = toPt) then

Exit;

X := (Max(fromPt.X, toPt.X) - Min(fromPt.X, toPt.X));

Y := (Max(fromPt.Y, toPt.Y) - Min(fromPt.Y, toPt.Y));

Result := Max(X, Y);

end;

 

function GetTPALineBetweenPoints(fromPt, toPt: TPoint): TPointArray;

var

i, h: integer;

e: Extended;

begin

if 3 > DistanceTo(fromPt, toPt) then

Exit;

h := Max(Round(Abs(fromPt.x - toPt.x)), Round(Abs(fromPt.y - toPt.y)));

SetLength(Result, (h - 1));

for i := 1 to (h - 1) do

begin

e := (i / h);

Result[(i - 1)].x := fromPt.x + Round((toPt.x - fromPt.x) * e);

Result[(i - 1)].y := fromPt.y + Round((toPt.y - fromPt.y) * e);

end;

end;

 

function GetTPALine(fromPt, toPt: TPoint): TPointArray;

var

i, h: integer;

e: Extended;

begin

h := Max(Round(Abs(fromPt.X - toPt.X)), Round(Abs(fromPt.Y - toPt.Y)));

SetLength(Result, h + 1);

for i := 0 to h do

begin

e := (i / h);

Result.X := fromPt.X + Round((toPt.X - fromPt.X) * e);

Result.Y := fromPt.Y + Round((toPt.Y - fromPt.Y) * e);

end;

end;

 

var

pt1, pt2: TPoint;

TPA: TPointArray;

h, i: Integer;

 

begin

ClearDebug;

pt1 := Point(0, 0);

pt2 := Point(10, 0);

WriteLn('GetTPALineBetweenPoints:');

TPA := GetTPALineBetweenPoints(pt1, pt2);

h := High(TPA);

for i := 0 to h do

WriteLn('TPA[' + IntToStr(i) + ']: ' + IntToStr(TPA.X) + ', ' + IntToStr(TPA.Y));

SetLength(TPA, 0);

WriteLn('');

WriteLn('GetTPALine:');

TPA := GetTPALine(pt1, pt2);

h := High(TPA);

for i := 0 to h do

WriteLn('TPA[' + IntToStr(i) + ']: ' + IntToStr(TPA.X) + ', ' + IntToStr(TPA.Y));

SetLength(TPA, 0);

end.[/scar]

 

-Jani

Edited by Janilabo
1 small fix for DistanceTo function.. Changed PointLine => GetTPALine and added GetTPALineBetweenPoints function.
Link to comment
Share on other sites

yeap that sounds great i will test it as soon as possible ....... at the moment we have hollyday and family comes 1st ...... but thank you very mutch 1 more reason i like scar is 1 powerful tool 2 for every problem there is a solution and 3 a good comunity with good users

may be freddy shout think about to integrate it in scar because it is a powerful minimap funktion . a special thx to janilabo i will integrate this funktion in every scrit i write in future thx a lot ... and happy eastern

--------------------------------------------

i made a test..... it works great! good job ^^

Edited by opiumhautopium1
Link to comment
Share on other sites

Hello opiumhautopium1,

 

Glad to hear it did work for you! Hopefully it did help too.

 

By the way, I made also another method for this, that you could always use..

It works with radius&angle (point "offset")..

 

Here is the alternative method (I made an example script for you):

 

[scar]function PointToStr(pt: TPoint): string;

begin

Result := (IntToStr(pt.X) + ', ' + IntToStr(pt.Y));

end;

 

function GetAngleBetweenPoints(pt1, pt2: TPoint): Extended;

begin

Result := Degrees(ArcTan2((pt2.Y - pt1.Y), (pt1.X - pt2.X)));

end;

 

procedure GetOffsetFromPoints(pt1, pt2: TPoint; var radius, angle: Extended);

var

X, Y: Integer;

begin

X := (pt1.X - pt2.X);

Y := (pt2.Y - pt1.Y);

radius := Round(Sqrt(Pow(X, 2) + Pow(Y, 2)));

angle := ((ArcTan2(Y, X) * 180) / Pi);

end;

 

procedure OffsetPoint(var Pt: TPoint; radius, angle: Extended);

begin

Pt.X := (Pt.X + Round(radius * Sin(Radians(angle - 90.0))));

Pt.Y := (Pt.Y + Round(radius * Cos(Radians(angle - 90.0))));

end;

 

function GetPointWithOffset(startPt: TPoint; radius, angle: Extended): TPoint;

begin

Result := Point(startPt.X + Round(radius * Sin(Radians(angle - 90.0))),

startPt.Y + Round(radius * Cos(Radians(angle - 90.0))));

end;

 

var

pt1, pt2: TPoint;

r, a: Extended;

 

begin

ClearDebug;

pt1 := Point(0, 0);

pt2 := Point(10, 0);

a := GetAngleBetweenPoints(pt1, pt2);

r := Distance(pt1.X, pt1.Y, pt2.X, pt2.Y);

WriteLn('RADIUS: ' + FloatToStr® + ', ANGLE: ' + FloatToStr(a) + ' [GetAngleBetweenPoints() + Distance()]');

GetOffsetFromPoints(pt1, pt2, r, a);

WriteLn('RADIUS: ' + FloatToStr® + ', ANGLE: ' + FloatToStr(a) + ' [GetOffSetFromPoints()]');

WriteLn(PointToStr(GetPointWithOffset(pt1, 7, a)));

OffsetPoint(pt1, 7, a);

WriteLn(PointToStr(pt1));

end.[/scar]

 

-Jani

Edited by Janilabo
Link to comment
Share on other sites

of course i will use it in every script because it is a great minimap waypoint funktion to keep the exact distance and i dont have to loop the procedure to check the distance so it can do other things ^^ happy eastern ^^

all seafight scripst handle 90% what to do over the minimap so it is realy realy realy good to have this function

Edited by opiumhautopium1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...