Hahahah what a coincidence. I have just used this @JohnBlackburne solution to solve my own problem when drawing ellipse based on mouse position and ellipse center point. So it pays out to even try to help even though you have no clue what you are doing π
When drawing ellipse I am using this code:
aX = p2.x - p1.x;
aY = p2.y - p1.y;
return Math.atan2(aY, aX);
to find out angle between 2 points (mouse point and ellipse center point). And I need this to determine current angle of drawing ellipse. So I compare angle between ellipse center point and current mouse point. I was also using this to determine direction of the ellipse. So if angle is greater than 0 than draw ellipse from bottom, otherwise draw from top (one half of ellipse and other half of ellipse).
But now I added a feature to set angle when drawing ellipse so that entire ellipse is rotated around its center by that angle. The problem was I can not longer rely on angle between points so I needed this formula to know when to draw other half of the ellipse π.
This was my code before:
protected function calculateEllipsePointFromMousePosition(xPos:Number, yPos:Number, ellipsePoint:Point)
{
_mousePoint.x = xPos;
_mousePoint.y = yPos;
_mouseAngle = MathUtils.getAngleBetweenPoints(
ellipseCenterPoint,
_mousePoint
);
_elipseX = Math.cos(_mouseAngle) * _elipseDrawXRadius;
_elipseY = MathUtils.calculateElipseY(
_elipseDrawXRadius,
_elipseDrawYRadius,
_elipseX
);
// this is the switch that draws other half of ellipse
if(_mouseAngle > 0)
{
_elipseY *= -1;
}
ellipsePoint.x = _elipseX * Math.cos(_elipseAngle) + _elipseY * Math.sin(_elipseAngle);
ellipsePoint.y = _elipseX * Math.sin(_elipseAngle) - _elipseY * Math.cos(_elipseAngle);
ellipsePoint.x = ellipsePoint.x + ellipseCenterPoint.x;
ellipsePoint.y = ellipsePoint.y + ellipseCenterPoint.y;
}
And this is my code now which works:
protected function calculateEllipsePointFromMousePosition(xPos:Number, yPos:Number, ellipsePoint:Point)
{
_mousePoint.x = xPos;
_mousePoint.y = yPos;
_mouseAngle = MathUtils.getAngleBetweenPoints(
ellipseCenterPoint,
_mousePoint
);
_elipseCurrentAngle = _mouseAngle - _elipseAngle;
_elipseX = Math.cos(_elipseCurrentAngle) * _elipseDrawXRadius;
_elipseY = MathUtils.calculateElipseY(
_elipseDrawXRadius,
_elipseDrawYRadius,
_elipseX
);
// this is John's solution only reverted to check if angle is more counter clockwise
if(Math.sin(_mouseAngle - _elipseAngle) > 0)
{
_elipseY *= -1;
}
ellipsePoint.x = _elipseX * Math.cos(_elipseAngle) + _elipseY * Math.sin(_elipseAngle);
ellipsePoint.y = _elipseX * Math.sin(_elipseAngle) - _elipseY * Math.cos(_elipseAngle);
ellipsePoint.x = ellipsePoint.x + ellipseCenterPoint.x;
ellipsePoint.y = ellipsePoint.y + ellipseCenterPoint.y;
}
public static function calculateElipseY(a:Number, b:Number, x:Number):Number
{
return b / a * (Math.sqrt(Math.pow(a, 2) - Math.pow(x, 2)));
}
I was banging my head so hard trying to solve this and I remember that John gave solution for determining direction of rotation so I used it to solve totally different problem π this was awesome π