How to draw a triangle with OnGUI from its sides and angles?

Hey guys, I was making a triangle solver program today, it all works fine and in the end returns all the sides and angles. I was wondering if it’s possible to draw this triangle from this information with OnGUI or Graphics and such. Should I try to rotate the gui and then draw a box?

–David

There isn’t an easy way to draw lines using the Unity GUI, however, you could do something similar with the LineRenderer class. You could even feed a method GUI coordinates if you wanted to keep your code consistent. Example:

using UnityEngine;
using System.Collections;

public class Triangle : MonoBehaviour
{
	void Start()
	{
		// Declare points in GUI space
		Vector2 p0 = new Vector2(50f, Screen.height-50f);
		Vector2 p1 = new Vector2(Screen.width-50f, Screen.height-50f);
		Vector2 p2 = new Vector2(Screen.width/2f, 50f);

		DrawTriangleInGUISpace(p0, p1, p2);
	}

	// Accepts GUI points, NOT screen space
	void DrawTriangleInGUISpace(
		Vector2 p0,
		Vector2 p1,
		Vector2 p2)
	{
		LineRenderer lr = gameObject.AddComponent<LineRenderer>();
		lr.useWorldSpace = true;
		lr.SetVertexCount(4);

		// Since there isn't a GUIToWorldPoint available at runtime, subtract y values by screen height to convert
		// from screen to GUI space.  ScreenToWorldPoint accepts a Vector3 arg, where Z is distance from camera. 
		Vector3 worldPoint0 = Camera.main.ScreenToWorldPoint(new Vector3(p0.x, Screen.height - p0.y, 10f));
		Vector3 worldPoint1 = Camera.main.ScreenToWorldPoint(new Vector3(p1.x, Screen.height - p1.y, 10f));
		Vector3 worldPoint2 = Camera.main.ScreenToWorldPoint(new Vector3(p2.x, Screen.height - p2.y, 10f));

		lr.SetPosition(0, worldPoint0);
		lr.SetPosition(1, worldPoint1);
		lr.SetPosition(2, worldPoint2);
		lr.SetPosition(3, worldPoint0);
	}
}

karl_, thanks for the answer, but I found a solution that works in OnGUI too. It uses a modified version of the DrawLine extension available from the unify wiki.

My result:

triangle

var SideA = 8.0;
var SideB = 6.0;
var SideC = 7.0;

function OnGUI () {

	var point1 : Vector2 = Vector2(0,0);
	var point2 : Vector2 = Vector2(SideA,0);
	var point3 : Vector2 = Vector2(0, 0);
	point3.x = (SideA*SideA+SideB*SideB-SideC*SideC)/(2*SideA); // calculates point3 using the circle intersection formula
	point3.y = -Mathf.Sqrt(SideB*SideB - point3.x*point3.x);

	var s : float = (SideA+SideB+SideC)/2;
	var h : float = (2/SideC)*Mathf.Sqrt(s*(s-SideA)*(s-SideB)*(s-SideC)); // calculates the height of the triangle using the Heron's formula. This isn't necessary, but in this script it's used for vertical centering

	point1 -= Vector2(SideA/2,-h/2); // center all the points in local space
	point2 -= Vector2(SideA/2,-h/2);
	point3 -= Vector2(SideA/2,-h/2);

	point1 *= 20; // scale the triangle
	point2 *= 20;
	point3 *= 20;

	point1 += Vector2(Screen.width/2,Screen.height/2); // center to Screen's center
	point2 += Vector2(Screen.width/2,Screen.height/2);
	point3 += Vector2(Screen.width/2,Screen.height/2);

	DrawLine(point1,point2,Color.white,1); // draw SideB
	DrawLine(point1,point3,Color.white,1); // draw SideA
	DrawLine(point2,point3,Color.white,1); // draw SideC
}

//modified version of the DrawLine script

static var lineTex : Texture2D;
static function DrawLine(pointA : Vector2, pointB : Vector2, color : Color, width : float) {
    var matrix = GUI.matrix;
    if (!lineTex) {
    	lineTex = Texture2D(1, 1);
    	lineTex.SetPixel(0, 0, Color.white);
    	lineTex.Apply();
    }
    var savedColor = GUI.color;
    GUI.color = color;
    var angle = Vector2.Angle(pointB-pointA, Vector2.right);
    if (pointA.y > pointB.y) { angle = -angle; }
    GUIUtility.RotateAroundPivot(angle, pointA);
    GUI.DrawTexture(new Rect(pointA.x, pointA.y, (pointB - pointA).magnitude, width), lineTex);
    GUI.matrix = matrix;
    GUI.color = savedColor;
}

–David