Using Splines for charts with smooth graph
This Arduino project demonstrates how Splines can be used to display real-world data on an E-paper screen.
Components and supplies
1
ESP32
Apps and platforms
1
Arduino IDE 2.0 (beta)
1
PlatformIO
Project description
Code
All functions
cpp
This is not the entire code.
1//These are all the different functions for the Splines and the Bezier Curve. 2//In order to plot the graph on any display call this in main(): 3/* 4for(double t = 0; t < dataLength - 1; t += 0.001){ 5 Point pixel; 6 pixel = [SplineFunction](arr,[if needed: extra arguments,] dataLength,t); 7 display.drawPixel(pixel.x, pixel.y); 8*/ 9 10struct Point{ 11 double x; 12 double y; 13}; 14 15//You also have to define a array of Points to pass to the functions. 16 17Point Bezier(Point p0,Point p1,Point p2,Point p3,double t){ 18 double x = p0.x + 19 t * (-3 * p0.x + 3 * p1.x) + 20 pow(t,2) * (3 * p0.x - 6 * p1.x + 3 * p2.x) + 21 pow(t,3) * (-1 * p0.x + 3 * p1.x - 3 * p2.x + p3.x); 22 23 double y = p0.y + 24 t * (-3 * p0.y + 3 * p1.y) + 25 pow(t,2) * (3 * p0.y - 6 * p1.y + 3 * p2.y) + 26 pow(t,3) * (-1 * p0.y + 3 * p1.y - 3 * p2.y + p3.y); 27 28 return {x,y}; 29} 30 31Point Hermite(Point* data, Point v0, Point v1, int dataLength, double t){ 32 int i = t; 33 34 Point p0 = data[i]; 35 Point p1 = data[i+1]; 36 static Point lastv1; 37 38 if(v0.x + v0.y + v1.x + v1.y == 0){ 39 Point pm1 = data[max(0,i-1)]; 40 Point p2 = data[min(dataLength,i+2)]; 41 v0 = {(p1.x - pm1.x) / 2 , (p1.y - pm1.y) / 2 }; 42 v1 = {(p2.x - p0.x) / 2 , (p2.y - p0.y) / 2 }; 43 } 44 else if(i > 0){ 45 v0 = lastv1; 46 lastv1 = v1; 47 } 48 49 50 t = t - (double)i; 51 double x = p0.x + 52 t * v0.x + 53 pow(t,2) * (-3 * p0.x - 2 * v0.x + 3 * p1.x - v1.x) + 54 pow(t,3) * (2 * p0.x + v0.x - 2 * p1.x + v1.x); 55 56 double y = p0.y + 57 t * v0.y + 58 pow(t,2) * (-3 * p0.y - 2 * v0.y + 3 * p1.y - v1.y) + 59 pow(t,3) * (2 * p0.y + v0.y - 2 * p1.y + v1.y); 60 61 return {x,y}; 62} 63 64 65 66 67 68Point CatmullRom(Point* data, int dataLength, double t){ 69 int i = t; 70 Point p0; 71 Point p1 = data[i]; 72 Point p2 = data[i+1]; 73 Point p3; 74 if(i == 0){ 75 p0 = p1; 76 } 77 else{ 78 p0 = data[i-1]; 79 } 80 if(i >= dataLength - 2){ 81 p3 = p2; 82 } 83 else{ 84 p3 = data[i+2]; 85 } 86 t = t - (double)i; 87 double x = (2 * p1.x + 88 t * (- p0.x + p2.x) + 89 pow(t,2) * (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) + 90 pow(t,3) * (- p0.x + 3 * p1.x - 3 * p2.x + p3.x)) / 2; 91 92 double y = (2 * p1.y + 93 t * (- p0.y + p2.y) + 94 pow(t,2) * (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) + 95 pow(t,3) * (- p0.y + 3 * p1.y - 3 * p2.y + p3.y)) / 2; 96 return {x,y}; 97} 98 99Point Cardinal(Point* data, double s, int dataLength, double t){ 100 int i = t; 101 Point p0; 102 Point p1 = data[i]; 103 Point p2 = data[i+1]; 104 Point p3; 105 if(i == 0){ 106 p0 = p1; 107 } 108 else{ 109 p0 = data[i-1]; 110 } 111 if(i >= dataLength - 2){ 112 p3 = p2; 113 } 114 else{ 115 p3 = data[i+2]; 116 } 117 t = t - (double)i; 118 double x = p1.x + 119 t * (- s * p0.x + s * p2.x) + 120 pow(t,2) * (2 * s * p0.x + (s-3) * p1.x + (3-2*s) * p2.x - s * p3.x) + 121 pow(t,3) * (- s * p0.x + (2-s) * p1.x + (s-2) * p2.x + s * p3.x); 122 123 double y = p1.y + 124 t * (- s * p0.y + s * p2.y) + 125 pow(t,2) * (2 * s * p0.y + (s-3) * p1.y + (3-2*s) * p2.y - s * p3.y) + 126 pow(t,3) * (- s * p0.y + (2-s) * p1.y + (s-2) * p2.y + s * p3.y); 127 128 return {x,y}; 129} 130 131Point BSpline(Point* data, int dataLength, double t){ 132 int i = t; 133 Point p0; 134 Point p1 = data[i]; 135 Point p2 = data[i+1]; 136 Point p3; 137 if(i == 0){ 138 p0 = p1; 139 } 140 else{ 141 p0 = data[i-1]; 142 } 143 if(i >= dataLength - 2){ 144 p3 = p2; 145 } 146 else{ 147 p3 = data[i+2]; 148 } 149 t = t - (double)i; 150 double x = (p0.x + 4 * p1.x + p2.x + 151 t * (-3 * p0.x + 3 * p2.x) + 152 pow(t,2) * (3 * p0.x - 6 * p1.x + 3 * p2.x) + 153 pow(t,3) * (- p0.x + 3 * p1.x - 3 * p2.x + p3.x)) / 6; 154 155 double y = (p0.y + 4 * p1.y + p2.y + 156 t * (-3 * p0.y + 3 * p2.y) + 157 pow(t,2) * (3 * p0.y - 6 * p1.y + 3 * p2.y) + 158 pow(t,3) * (- p0.y + 3 * p1.y - 3 * p2.y + p3.y)) / 6; 159 160 return {x,y}; 161}
Comments
Only logged in users can leave comments