-
Notifications
You must be signed in to change notification settings - Fork 14
/
Derivatives.cs
48 lines (43 loc) · 1.56 KB
/
Derivatives.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using UnityEngine;
public class Derivatives : MonoBehaviour
{
Material _Red, _Blue, _Black;
float Function (float x)
{
return 0.5f * Mathf.Sin(x) * x; // derivative = 0.5f * (Mathf.Sin(x) + x * Mathf.Cos(x))
}
float Derivative (float x, float h)
{
return (Function(x + h) - Function(x - h)) / (2f * h);
}
void Start()
{
_Red = new Material(Shader.Find("Legacy Shaders/Diffuse"));
_Red.color = Color.red;
_Blue = new Material(Shader.Find("Legacy Shaders/Diffuse"));
_Blue.color = Color.blue;
_Black = new Material(Shader.Find("Legacy Shaders/Diffuse"));
_Black.color = Color.black;
float stepSize = 0.1f;
for (float x = -16f; x < 16f; x += stepSize)
{
GameObject red = GameObject.CreatePrimitive(PrimitiveType.Sphere);
red.transform.position = new Vector3(x, 0f, Function(x));
red.GetComponent<Renderer>().sharedMaterial = _Red;
//////////////////////////////////////////////////////////////////////////
GameObject blue = GameObject.CreatePrimitive(PrimitiveType.Sphere);
blue.transform.position = new Vector3(x, 0f, Derivative(x, stepSize));
blue.GetComponent<Renderer>().sharedMaterial = _Blue;
//////////////////////////////////////////////////////////////////////////
GameObject black = GameObject.CreatePrimitive(PrimitiveType.Sphere);
black.transform.position = new Vector3(x, 0f, 0.5f * (Mathf.Sin(x) + x * Mathf.Cos(x))); // check
black.GetComponent<Renderer>().sharedMaterial = _Black;
}
}
void OnDestroy()
{
Destroy(_Red);
Destroy(_Blue);
Destroy(_Black);
}
}