Reputation: 27286
I have this formula in a function below. It's a fairly simple concept, yet this formula took me almost 2 weeks to get perfect. What it does is calculates what point to place an object at a given degree around and distance from a central point. It's useful for manually drawing circles, and also I primarily use it for a needle gauge component of mine. It calculates where to draw the needle.
Now I'm trying to figure out how to modify this formula to take ovals or ellipses into account. I did think of the idea of drawing a component a round shape first, and then stretching it after everything's drawn, but this is not a clean solution, as the drawing which I'm doing will already be in the shape of an oval.
I need to add just one parameter to this function to tell it the ratio between the width/height so it knows how to off-set this point. By default, this parameter should be 1, meaning Width=Height, meaning no ovalish drawing or offset. But suppose I put 2, which means width is twice the size of the height, or 1.5 would mean the width is 1.5 times the height.
Here's the original function:
function NewPosition(Center: TPoint; Distance: Integer; Degrees: Single): TPoint;
var
Radians: Real;
begin
//Convert angle from degrees to radians; Subtract 135 to bring position to 0 Degrees
Radians:= (Degrees - 135) * Pi / 180;
Result.X:= Trunc(Distance*Cos(Radians)-Distance*Sin(Radians))+Center.X;
Result.Y:= Trunc(Distance*Sin(Radians)+Distance*Cos(Radians))+Center.Y;
end;
Here it is with the added parameter I need:
function NewPosition(Center: TPoint; Distance: Integer; Degrees: Single;
OvalOffset: Single = 1): TPoint;
var
Radians: Real;
begin
//Convert angle from degrees to radians; Subtract 135 to bring position to 0 Degrees
Radians:= (Degrees - 135) * Pi / 180;
Result.X:= Trunc(Distance*Cos(Radians)-Distance*Sin(Radians))+Center.X;
Result.Y:= Trunc(Distance*Sin(Radians)+Distance*Cos(Radians))+Center.Y;
end;
DEFINITIONS:
Upvotes: 6
Views: 2280
Reputation: 1440
Add a division by OvalOffset
to just the Result.Y
formula...
Result.Y:= Trunc((Distance*Sin(Radians)+Distance*Cos(Radians))/OvalOffset)
+Center.Y;
Upvotes: 6