# Why don't Sin and Cos give expected results when rotating off axis around a non uniform shape?

When I rotate a rectangle and draw straight lines along it's edge using Sin and Cos the lines don't follow the edge as the angle approaches 45 degrees, why? The exact same code works just fine when I use it on a square. See Gif: Left object has the same width and height, No distortion of angles. Right side has larger X axis and lots of distortion.

Why does this happen and how do I prevent it?

Unity code:

[RequireComponent(typeof(Image))]
class rotation : MonoBehaviour
{
[SerializeField]
private float left = 180f, up = 90f;

private void OnDrawGizmosSelected()
{
RectTransform rect = GetComponent<RectTransform>();

Vector3[] corners = new Vector3;
rect.GetWorldCorners(corners);
float rot = transform.eulerAngles.z;

Gizmos.color = Color.red;
Gizmos.DrawLine(corners, corners + new Vector3(
Mathf.Cos((left + rot) * Mathf.Deg2Rad) * rect.rect.width,
Mathf.Sin((left + rot) * Mathf.Deg2Rad) * rect.rect.height));

Gizmos.color = Color.blue;
Gizmos.DrawLine(corners, corners + new Vector3(
Mathf.Cos((up + rot) * Mathf.Deg2Rad) * rect.rect.width,
Mathf.Sin((up + rot) * Mathf.Deg2Rad) * rect.rect.height));
}
}

• Question, wouldn't left be 90? – ErnieDingo Nov 22 '17 at 22:37
• yes... it would be. Fixed – user3797758 Nov 22 '17 at 22:38
• How did that affect the problem? I assume the lines on the right are now rotating at a 90 degree angle. – ErnieDingo Nov 22 '17 at 22:42
• well in the Unity project I already had it set to 90... The problem can be fixed if I hardcode the rect Width/Height to a value or just use the same one to draw a line but if I wanted to draw a line at 45 degrees it wouldn't work – user3797758 Nov 22 '17 at 22:45
• The other option i suggest is to possibly move to matrices. I always now find it easier to work with them, it seems a bit heavy handed. But you can apply it to all your points around your rotation point (the corner) and get a consistent approach. – ErnieDingo Nov 22 '17 at 22:52

The length of the lines must always equal the length of the side. If you however multiply sin(angle) by the width of the rect and cos(angle) by the height, then if the angle point upwards, then the length of the line will be equal to the width of the rectangle and if the angle points sideways, then the length of the line will be equal to the height of the rectangle.

Gizmos.DrawLine(corners, corners + new Vector3(
Mathf.Cos((left + rot) * Mathf.Deg2Rad) * rect.rect.width,
Mathf.Sin((left + rot) * Mathf.Deg2Rad) * rect.rect.width)); // Change is here

Gizmos.color = Color.blue;
Gizmos.DrawLine(corners, corners + new Vector3(
Mathf.Cos((up + rot) * Mathf.Deg2Rad) * rect.rect.height, // Change is here
Mathf.Sin((up + rot) * Mathf.Deg2Rad) * rect.rect.height));

• Yes that would work but what if I want to draw a line at 45 degrees? The line wouldn't have a consistent angle but would shift while rotating. Is that something that Sin and Cos would even work on? – user3797758 Nov 22 '17 at 22:47
• @user3797758 it seems like you don't understand what sin and cos is (please look it up). If you want the line to keep it's length, then you need to multiply both the results of sin and cos with the same value. – Bálint Nov 22 '17 at 22:50
• That's the thing that I don't want it to keep it's length but rather keep the drawn angle consistent, I'm trying to draw an ellipse. imgur.com/a/nSnHm – user3797758 Nov 22 '17 at 22:59
• @user3797758 Have you actually tried my answer? It should fix the problem. – Bálint Nov 22 '17 at 23:01
• Yes that fixes the question but how do you render an eclipse in a rotating shape, I suppose I should ask that In a separate question... – user3797758 Nov 22 '17 at 23:07