Modify a formula from calculating around a circle to around an oval?
Asked Answered
D

1

6

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

enter image description here

Duggins answered 8/12, 2011 at 15:33 Comment(12)
Are the ellipse axes of symmetry horizontal and vertical?Belgian
Yes, I don't care about rotation of the oval, just width differing from height.Duggins
Just to clarify: the result from NewPosition shall have the given Distance from the center and be on the angle given by Degrees? At least that is what I read from your DEFINITIONS.Excursionist
Exactly. I already have this working, no more help needed, unless you want to do something even fancier with it? Such as, add ability to rotate this oval to a certain degree? Not important though, thanks.Duggins
In that case your question is somewhat unclear. Given a fixed distance and varying degrees to the function, most of the resulting points will have a different distance to the center as specified. For an elliptic curve the values for Distance and Degree usually don't match the measured distance and degree of each point.Excursionist
I added a picture to show an example of how it works now and what I want it to do (even though my question has already been answered)Duggins
Actually, when looking at the picture, it's very reasonable to say that what I call Distance is actually considered the Radius of this circle, while Degrees is of course the Angle. I should have paid more attention in my Math classes :PDuggins
oval or ellipse?Belike
Other than the reference to 'Oval' instead of 'Ellipse', I don't see any reason for this question to deserve a down-vote - I swear, there are really some haters here.Duggins
Without being the down-voter: The fact is, that if you enter the values labeled 2 and 3 from the elliptic drawing into the function, you won't get the point labeled 4 as the result. To get the desired result labeled 4 you have to enter different values for distance and degrees than those shown in the drawing.Excursionist
Sorry I still don't understand what you're trying to say. This function has always worked just fine, even after adding this new parameter (as answered below) I am getting the results I am wanting. If you mean Distance 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 new OvalOffset parameter basically turns the circle into an ellipse.Duggins
Your definition for degree is not valid. Simply because you used the term degree in the definitionBattologize
D
6

Add a division by OvalOffset to just the Result.Y formula...

Result.Y:= Trunc((Distance*Sin(Radians)+Distance*Cos(Radians))/OvalOffset)
           +Center.Y;
Darrin answered 8/12, 2011 at 15:47 Comment(5)
Perhaps I got it wrong, but with your code won't the distance between Result and Center be different to the given value Distance?Excursionist
Distance = how far away from center regardless of degrees, Degrees = degrees around center point starting from up-rightDuggins
And yes I tried your formula and instead of making an oval, it made just a smaller circle (I used exact formula on Result.X too)Duggins
@Jerry How could a linear scaling of the Y offset result in a circle?Belgian
Ah ha, I misunderstood the answer because it wasn't thoroughly explained. I tried to copy this formula over to Result.X as well, which resulted in just a smaller circle. Changed Result.X back to original code, and it works now :DDuggins

© 2022 - 2024 — McMap. All rights reserved.