Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify a formula from calculating around a circle to around an oval?

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

like image 644
Jerry Dodge Avatar asked Dec 08 '11 15:12

Jerry Dodge


People also ask

What is the formula for the circumference of an oval?

Circumference = π × (a + b)[1 + (3 × h/(10 + √(4 - 3h)))] .

Is there a formula for an oval?

One-half of the length of the minor axis. The equation of an ellipse written in the form (x−h)2a2+(y−k)2b2=1. The center is (h,k) and the larger of a and b is the major radius and the smaller is the minor radius.

Is a circle an oval?

Yes, a circle is different from an oval shape. A circle is a two-dimensional geometrical shape that is formed by a set of points that are at a fixed distance from the center. However, an oval shape does not have a common distance from the center (radius).


1 Answers

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

Result.Y:= Trunc((Distance*Sin(Radians)+Distance*Cos(Radians))/OvalOffset)
           +Center.Y;
like image 200
Toto Avatar answered Oct 18 '22 23:10

Toto