Guest User

Unity3D Graphing Calculator\

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