-
Notifications
You must be signed in to change notification settings - Fork 3
/
LerpXY2RTH.cpp
130 lines (103 loc) · 2.99 KB
/
LerpXY2RTH.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "raylib.h"
#include "raymath.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
//Convert Cartesian to polar
Vector2 conv(Vector2 in) {
Vector2 out;
out.x=sqrt(in.x*in.x+in.y*in.y);
out.y=atan2(in.y,in.x);
return out;
}
//Convert polar to Cartesian
Vector2 ppol(Vector2 in) {
Vector2 out;
out.x=in.x*cos(in.y);
out.y=in.x*sin(in.y);
return out;
}
//Simple LERP function for vector2
Vector2 VLerp(Vector2 a, Vector2 b, float l) {
Vector2 out;
out.x=(1.0-l)*a.x+l*b.x;
out.y=(1.0-l)*a.y+l*b.y;
return out;
}
int main()
{
//Initialize Raylib
InitWindow(1000, 1000, "Title");
SetWindowPosition(600,50);
Camera2D camera = { 0 };
camera.target = (Vector2){ 0, 0 };
camera.offset = (Vector2){ 0, 0 };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
SetTargetFPS(30);
vector<Vector2> pc; //Holds points that make up the Cartesian grid lines
vector<Vector2> rt; //Points of the above after transformation to polar
vector<Vector2> fc; //Points of the function plotted in Cartesian space
vector<Vector2> fr; //Points of the above tranformed to R-Theta polar
float step=0.005;
//Draw vertical grid lines
for (float x=-8;x<=8;x+=.5)
{
for (float y=-8;y<8;y+=step){
pc.push_back((Vector2){x,y});
}
}
//Draw horizontal grid lines
for (float y=-8;y<=8;y+=.5)
{
for (float x=-8;x<8;x+=step){
pc.push_back((Vector2){x,y});
}
}
//Transform all those points to polar coordinates
for (int i=0;i<pc.size();i++) {
rt.push_back(ppol(pc[i]));
}
//Point for the function we wish to plot
for (float x=-4;x<4.0;x+=step/8.0) {
fc.push_back({x,2.0*sin(x*2.0*PI)});
//fc.push_back({x,sqrt(4-x*x)});
//fc.push_back({x,-1.0*sqrt(4-x*x)});
}
//The above transformed into polar
for (int i=0;i<fc.size();i++) {
fr.push_back(ppol(fc[i]));
}
float time=0.0;
Vector2 temp;
while (!WindowShouldClose()){
//Update
if (time<=1.0) {
time+=0.001;
}
//Draw
BeginDrawing();
ClearBackground(BLACK);
BeginMode2D(camera);
for (int i=0;i<rt.size();i++) {
temp=VLerp(pc[i],rt[i],time); //Simply lerp between X-Y and R-Theta points defined above
temp=Vector2Scale(temp,50); //Scaled everything up by 50
temp=Vector2Add(temp,{500,500}); //And centered it on the display
DrawPixelV(temp,RED);
}
for (int i=0;i<fr.size();i++) { //Did the same for the function
temp=VLerp(fc[i],fr[i],time);
temp=Vector2Scale(temp,50);
temp=Vector2Add(temp,{500,500});
DrawPixelV(temp,YELLOW);
}
EndMode2D();
DrawFPS(10,10);
EndDrawing();
}
return 0;
}
//There are a jillion ways to make this better. I just wanted to post it because it's fun to watch