SRL Archive - Research & Development Lounge

Fizzi's Compass Angle Function


Fizzi

2007-08-21 22:33:47

Well, the function by Freddy isn't working atm (always returns as 268), and we need it badly for KYAB. I didn't feel like waiting, so I made my own.

I'll add a description for how it works soon, need to do something first tho :).
//Returns the compass angle in degrees going clockwise. By Fizzi
function FizCompassAngle : integer;
var
x1, y1, x2, y2, x3, y3, x4, y4, dx, dy, xt, yt : integer;
StartAngle, PerfectTest, theta : integer;
UseQ3 : boolean;
begin
//Quadrant 1: 562, 3 to 578, 19
//Quadrant 2: 544, 3 to 560, 19
//Quadrant 3: 544, 21 to 560, 38
//Quadrant 4: 562, 21 to 578, 38
//center : 561, 20
PerfectTest := 4;
if FindColorSkipBox(x1, y1, 920735, 562, 2, 579, 19, IntToBox(561, 11, 566, 20)) then
begin
Writeln('Found point in Quadrant 1');
PerfectTest := PerfectTest - 1;
end;
if FindColorSkipBox(x2, y2, 920735, 543, 2, 560, 19, IntToBox(552, 11, 561, 20)) then
begin
Writeln('Found point in Quadrant 2');
PerfectTest := PerfectTest - 1;
end;
if FindColorSkipBox(x3, y3, 920735, 543, 21, 560, 39, IntToBox(552, 20, 561, 26)) then
begin
Writeln('Found point in Quadrant 3');
PerfectTest := PerfectTest - 1;
end;
if FindColorSkipBox(x4, y4, 920735, 562, 21, 579, 39, IntToBox(561, 20, 566, 26)) then
begin
Writeln('Found point in Quadrant 4');
PerfectTest := PerfectTest - 1;
end;
if(PerfectTest > 1) then
begin
if not(FindColor(xt, yt, 920735, 561-2, 3+1, 561+2, 3+5)) then
begin
Writeln('Found perfect angle 0!');
Result := 0;
Exit;
end;
if not(FindColor(xt, yt, 920735, 578-5, 20-2, 578+1, 20+2)) then
begin
Writeln('Found perfect angle 90!');
Result := 90;
Exit;
end;
if not(FindColor(xt, yt, 920735, 561-2, 38-5, 561+2, 38+1)) then
begin
Writeln('Found perfect angle 180!');
Result := 180;
Exit;
end;
if not(FindColor(xt, yt, 920735, 544-1, 20-2, 544+5, 20+2)) then
begin
Writeln('Found perfect angle 270!');
Result := 270;
Exit;
end;
end else
begin
if(x1 = 0) and (y1 = 0) then
begin
StartAngle := 0;
UseQ3 := True;
end;
if(x2 = 0) and (y2 = 0) then StartAngle := 270;
if(x3 = 0) and (y3 = 0) then StartAngle := 180;
if(x4 = 0) and (y4 = 0) then StartAngle := 90;
if not(UseQ3) then
begin
dx := x1 - 561;
dy := y1 - 20;
theta := (Round(Degrees(ArcTan(dx/dy))))*(-1);
Writeln('Found tilted angle ' + inttostr(StartAngle + theta) + '!');
Result := (StartAngle + theta);
Exit;
end else
begin
dx := 561 - x3;
dy := 20 - y3;
theta := (Round(Degrees(ArcTan(dx/dy))))*(-1);
Writeln('Found tilted angle ' + inttostr(StartAngle + theta) + '!');
Result := (StartAngle + theta);
Exit;
end;
end;
Result := -1;
end;

Bobarkinator

2007-08-21 22:35:57

Very nice again, and I will say it again, you are an amazing scripter. This was needed. I have not tested yet but I'm sure it works. Even if it doesn't that is some great code.

Santa_Clause

2007-08-24 05:53:35

if not(FindColor(xt, yt, 920735, 561-2, 3+1, 561+2, 3+5)) then
begin

Erm..instead of 561 - 2 couldn't you just do 559, and so on?

The Claw

2007-08-24 08:53:26

Good stuff. We're learning about the unit circle and trig equations atm in school, so give it a week and I'll be able to do this stuff too, lol.

ZephyrsFury

2007-08-24 09:01:06

Lol they're easy. :p Just wait until you get to Year 11 and start with Trig Identities and Solving trig equations….

Oh and BTW, I'm testing it right now. :D

SKy Scripter

2007-08-24 09:17:29

Heres Mine :) I Like It…

function DetectCompassAngle : Extended;
var
P : TPointArray;
I, Mx, My, Midx, Midy, L : Integer;
begin
Midx :=561;
Midy :=20;
FindColorsTolerance(P, 1911089, 539, 0, 581, 38, 0);
L := Length(P);
if (l = 0) then exit;
for i := 0 to L-1 do
begin
Mx := Mx + P[i].x;
My := My + P[i].y;
end;
Mx := Mx / L;
My := My / L;
Result := FixD(FixD(Degrees(ArcTan2( my - midy, mx - midx))) + 90);
end;

The Claw

2007-08-24 10:30:18

From: ZephyrsFury
Lol they're easy. :p Just wait until you get to Year 11 and start with Trig Identities and Solving trig equations….

Oh and BTW, I'm testing it right now. :D


I'm in year 11, and we just learnt those today lolz. And you're right, they are pretty easy ;)

Fizzi

2007-08-24 11:02:37

From: SantaClause
if not(FindColor(xt, yt, 920735, 561-2, 3+1, 561+2, 3+5)) then
begin

Erm..instead of 561 - 2 couldn't you just do 559, and so on?


Yes, I did the additions/subtractions just cause it made it easier for me to see if I made any mistakes.

Freddy1990

2007-08-24 11:14:11

Mine works very well, but in SCAR 3.12 which will be one of the biggest updates so far

Janilabo

2007-08-24 11:17:53

From: Freddy1990
Mine works very well, but in SCAR 3.12 which will be one of the biggest updates so far
I can't wait. (I know, I just have to) :D

Fizzi

2007-08-26 16:41:15

From: SKy Scripter
Heres Mine :) I Like It…

function DetectCompassAngle : Extended;
var
P : TPointArray;
I, Mx, My, Midx, Midy, L : Integer;
begin
Midx :=561;
Midy :=20;
FindColorsTolerance(P, 1911089, 539, 0, 581, 38, 0);
L := Length(P);
if (l = 0) then exit;
for i := 0 to L-1 do
begin
Mx := Mx + P[i].x;
My := My + P[i].y;
end;
Mx := Mx / L;
My := My / L;
Result := FixD(FixD(Degrees(ArcTan2( my - midy, mx - midx))) + 90);
end;


I think yours wins Sky ;)