Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DavidDebnar · Jun 11, 2013 at 04:17 PM · guionguitriangle

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DavidDebnar · Jun 11, 2013 at 06:22 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by karl_ · Jun 11, 2013 at 04:41 PM

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);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How completly remove GUI.Repaint() calls ? 1 Answer

Free Gui solutions 2 Answers

Unity 4.7 - OnGUI prevent click/touch through 0 Answers

OnGUI button created by a foreach loop 4 Answers

GUI Placement Question. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges