0
\$\begingroup\$

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:

enter image description here

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[4];
        rect.GetWorldCorners(corners);
        float rot = transform.eulerAngles.z;

        Gizmos.color = Color.red;
        Gizmos.DrawLine(corners[3], corners[3] + 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[3], corners[3] + new Vector3(
            Mathf.Cos((up + rot) * Mathf.Deg2Rad) * rect.rect.width,
            Mathf.Sin((up + rot) * Mathf.Deg2Rad) * rect.rect.height));
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Question, wouldn't left be 90? \$\endgroup\$
    – ErnieDingo
    Nov 22, 2017 at 22:37
  • \$\begingroup\$ yes... it would be. Fixed \$\endgroup\$ Nov 22, 2017 at 22:38
  • \$\begingroup\$ How did that affect the problem? I assume the lines on the right are now rotating at a 90 degree angle. \$\endgroup\$
    – ErnieDingo
    Nov 22, 2017 at 22:42
  • \$\begingroup\$ 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 \$\endgroup\$ Nov 22, 2017 at 22:45
  • \$\begingroup\$ 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. \$\endgroup\$
    – ErnieDingo
    Nov 22, 2017 at 22:52

1 Answer 1

1
\$\begingroup\$

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.

Do this instead:

Gizmos.DrawLine(corners[3], corners[3] + 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[3], corners[3] + new Vector3(
      Mathf.Cos((up + rot) * Mathf.Deg2Rad) * rect.rect.height, // Change is here
      Mathf.Sin((up + rot) * Mathf.Deg2Rad) * rect.rect.height));
\$\endgroup\$
5
  • \$\begingroup\$ 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? \$\endgroup\$ Nov 22, 2017 at 22:47
  • \$\begingroup\$ @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. \$\endgroup\$
    – Bálint
    Nov 22, 2017 at 22:50
  • \$\begingroup\$ 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 \$\endgroup\$ Nov 22, 2017 at 22:59
  • \$\begingroup\$ @user3797758 Have you actually tried my answer? It should fix the problem. \$\endgroup\$
    – Bálint
    Nov 22, 2017 at 23:01
  • \$\begingroup\$ 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... \$\endgroup\$ Nov 22, 2017 at 23:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .