-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathIntegrals.cs
More file actions
54 lines (49 loc) · 1.54 KB
/
Integrals.cs
File metadata and controls
54 lines (49 loc) · 1.54 KB
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
49
50
51
52
53
54
using UnityEngine;
public class Integrals : MonoBehaviour
{
Material _Red, _White;
float Function (float x)
{
return 0.5f * (Mathf.Sin(x) + x * Mathf.Cos(x)); // integral = 0.5f * Mathf.Sin(x) * x
}
// Indefinite integrals (antiderivatives);
// Depends on input function, results can have value offset (Constant of Integration);
float Integral (float x, int n)
{
float stepSize = x / n;
float integral = 0f;
for (int i = 0; i < n; i++)
{
float x0 = i * stepSize;
float x1 = x0 + stepSize;
float y0 = Function(x0);
float y1 = Function(x1);
integral += 0.5f * stepSize * (y0 + y1);
}
return integral;
}
void Start()
{
_Red = new Material(Shader.Find("Legacy Shaders/Diffuse"));
_Red.color = Color.red;
_White = new Material(Shader.Find("Legacy Shaders/Diffuse"));
_White.color = Color.white;
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, 0.5f * Mathf.Sin(x) * x); // check
red.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
red.GetComponent<Renderer>().sharedMaterial = _Red;
GameObject white = GameObject.CreatePrimitive(PrimitiveType.Sphere);
white.transform.position = new Vector3(x, 0f, Integral(x, 100));
white.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
white.GetComponent<Renderer>().sharedMaterial = _White;
}
}
void OnDestroy()
{
Destroy(_Red);
Destroy(_White);
}
}