Guest User

Unity3D Graphing Calculator

a guest
Apr 1st, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Graph : MonoBehaviour {
  5.    
  6.     Vector3 zero = new Vector3(0, 0, 0);
  7.     Vector3 zero2 = new Vector3(0, 0, 0);
  8.     //Line 1
  9.     string slp;
  10.     string ynt;
  11.     //Line 2
  12.     string slp2;
  13.     string ynt2;
  14.    
  15.  
  16.     string currentMenu = "GUI";
  17.     public GameObject line1;
  18.     public GameObject line2;
  19.  
  20.     void Start() {
  21.         slp = "";
  22.         ynt = "";
  23.  
  24.         slp2 = "";
  25.         ynt2 = "";
  26.     }
  27.  
  28.     void OnGUI() {
  29.         if(currentMenu == "GUI")
  30.             GraphUI();
  31.     }
  32.  
  33.     void GraphUI() {
  34.  
  35.         GUI.Box(new Rect(0, 0, 300, Screen.height), "Graphing Calculator");
  36.         //Line 1
  37.         GUI.Label(new Rect(10, 30, 100, 20), "Y-Intercept 1");
  38.         ynt = GUI.TextField(new Rect(10, 50, 280, 60), ynt);
  39.         GUI.Label(new Rect(10, 120, 100, 20), "Slope 1");
  40.         slp = GUI.TextField(new Rect(10, 140, 280, 60), slp);
  41.         //Line 2
  42.  
  43.         GUI.Label(new Rect(10, 240, 100, 20), "Y-Intercept 2");
  44.         ynt2 = GUI.TextField(new Rect(10, 260, 280, 60), ynt2);
  45.         GUI.Label(new Rect(10, 330, 100, 20), "Slope 2");
  46.         slp2 = GUI.TextField(new Rect(10, 350, 280, 60), slp2);
  47.  
  48.         if(GUI.Button(new Rect(10, 420, 280, 60), "Calculate and Graph")) {
  49.             if((slp != null && slp != "") && (ynt != null && ynt != "")) {
  50.                 Calculate(slp, ynt, slp2, ynt2);
  51.             } else {
  52.                 Debug.LogWarning("Parameters Not Complete");
  53.             }
  54.         }
  55.  
  56.         if(GUI.Button(new Rect(10, Screen.height - 70, 280, 60), "Reset Graph")) {
  57.             slp = "";
  58.             ynt = "";
  59.             slp2 = "";
  60.             ynt2 = "";
  61.  
  62.             GameObject[] points = GameObject.FindGameObjectsWithTag("GraphPoint");
  63.             foreach (GameObject target in points) {
  64.                 GameObject.Destroy(target);
  65.             }
  66.         }
  67.     }
  68.  
  69.     //Line 1
  70.     void Calculate(string sl, string yt, string sl2, string yt2) {
  71.         int nextNumber = 1;
  72.         StartCoroutine(Calculate2(sl2, yt2));
  73.         float slope = float.Parse(sl);
  74.         float yint = float.Parse(yt);
  75.         float y = yint;
  76.         float x = slope;
  77.         zero.y = y;
  78.         zero.x = 0;
  79.  
  80.         for(int i = 0;i < 5;i++) {
  81.             Object redPoint = Instantiate(line1, zero, Quaternion.identity);
  82.             redPoint.name = "Red" + nextNumber;
  83.             nextNumber++;
  84.             zero.y = zero.y + slope;
  85.             zero.x = zero.x += 1;
  86.         }
  87.  
  88.         zero.y = 0;
  89.         zero.x = 0;
  90.         nextNumber = 1;
  91.         //CalculateAngle(GameObject.Find("Red1"), GameObject.Find("Red2"));
  92.         CalculateAngle(GameObject.Find("Red1"), GameObject.Find("Red2"));
  93.     }
  94.  
  95.     //Line 2
  96.     IEnumerator Calculate2(string sl, string yt) {
  97.         yield return new WaitForSeconds(0.0f);
  98.         int nextNumber = 1;
  99.         float slope = float.Parse(sl);
  100.         float yint = float.Parse(yt);
  101.         float y = yint;
  102.         float x = slope;
  103.         zero2.y = y;
  104.         zero2.x = 0;
  105.  
  106.         for(int i = 0;i < 5;i++) {
  107.             Object yellowPoint = Instantiate(line2, zero2, Quaternion.identity);
  108.             yellowPoint.name = "Yellow" + nextNumber;
  109.             nextNumber++;
  110.             zero2.y = zero2.y + slope;
  111.             zero2.x = zero2.x += 1;
  112.         }
  113.         zero2.y = 0;
  114.         zero2.x = 0;
  115.         nextNumber = 1;
  116.     }
  117.  
  118.     void CalculateAngle(GameObject x, GameObject y) {
  119.         float xDis = y.transform.position.x - x.transform.position.x;
  120.         float yDis = y.transform.position.y - x.transform.position.y;
  121.        
  122.         Debug.Log(xDis + " : " + yDis);
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment