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:
- Center = Central point where to base calculations from (center of ellipse)
- Distance = How far from Center in any direction, regardless of Degrees
- Degrees = How many degrees around central point, starting from up-right
- OvalOffset = Ratio of difference between Width and Height
Distance
is actually considered theRadius
of this circle, whileDegrees
is of course theAngle
. I should have paid more attention in my Math classes :P – DugginsDistance
will be altered due to a NEW sizing of the ellipse, then yes, this I know. But keeping all the same parameters and changing just the newOvalOffset
parameter basically turns the circle into an ellipse. – Duggins