Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bound a circle inside an ellipse?

Tags:

People also ask

How do you find if a point is inside an ellipse?

The region (disk) bounded by the ellipse is given by the equation: (x−h)2r2x+(y−k)2r2y≤1. So given a test point (x,y), plug it in (1). If the inequality is satisfied, then it is inside the ellipse; otherwise it is outside the ellipse.

Is ellipse a bounded curve?

Application: the boundary of the trace of a light cone on a wall is elliptic, if it is bounded. but, more generally, any bounded section of a quadric by a plane is an ellipse.

What scaling transformation changes a circle to an ellipse?

It is certainly possible to transform a circle into an ellipse. For example, using a non-uniform scaling with scaleX=2.0 and scaleY=1.0, you will get (a,b,c,d)=(2,0,0,1) and the ellipse equation will be (x'/2)^2+y'^2=z. However, the transformation matrix between a circle and an ellipse is not unique.


The title for this post was quite hard to think of, so if you can think of a more descriptive title please tell me. Anyway, my problem is quite specific and requires some simple maths knowledge. I am writing a C# WinForms application which is a bit like the old 'xeyes' Linux application. It basically is a set of eyes which follow around your mouse cursor. This may sound easy at first, however can get rather complicated if you're a perfectionist like me :P. This is my code so far (only the paint method, that is called on an interval of 16).

int lx = 35;
int ly = 50;
int rx;
int ry;

int wx = Location.X + Width / 2;
int wy = Location.Y + Height / 2;

Rectangle bounds = Screen.FromControl(this).Bounds;

// Calculate X

float tempX = (mx - wx) / (float)(bounds.Width / 2);

// Calculate Y

float tempY = (my - wy) / (float)(bounds.Height / 2);

// Draw eyes

e.Graphics.FillEllipse(Brushes.LightGray, 10, 10, 70, 100);
e.Graphics.FillEllipse(Brushes.LightGray, 90, 10, 70, 100);

// Draw pupils (this only draws the left one)

e.Graphics.FillEllipse(Brushes.Black, lx += (int)(25 * tempX), ly += (int)(40 * tempY), 20, 20);

Now this does work at a basic level, however sometimes this can happen if the user puts the cursor at 0,0.

The pupil can exit the main eye.

Now my question is how to fix this? What would the IF statement be to check where the mouse pointer is, and then reduce the pupil X depending on that?

Thanks.

Edit: This is where I get the mouse positions (my and mx):

private void timer_Tick(object sender, EventArgs e)
{
    mx = Cursor.Position.X;
    my = Cursor.Position.Y;

    Invalidate();
}

The timer is started in the eyes_Load event and the interval is 16.

Edit 2: Final solution: http://pastebin.com/fT5HfiQR