robert@493
|
1 /*
|
robert@493
|
2 * vector_graphics.cpp
|
robert@493
|
3 *
|
robert@493
|
4 * Created on: Nov 10, 2014
|
robert@493
|
5 * Author: parallels
|
robert@493
|
6 */
|
robert@493
|
7
|
robert@493
|
8 #include <cmath>
|
robert@493
|
9
|
robert@493
|
10 // Draw a line between two points at a specified rate in
|
robert@493
|
11 // pixels per buffer sample. Indicate maximum available space.
|
robert@493
|
12 // Returns space used
|
robert@493
|
13 int renderLine(float x1, float y1, float x2, float y2, float speed,
|
robert@493
|
14 float *buffer, int maxLength) {
|
robert@493
|
15 // Figure out length of line and therefore how many samples
|
robert@493
|
16 // are needed to represent it based on the speed (rounded to nearest int)
|
robert@493
|
17 float totalLineLength = sqrtf((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
|
robert@493
|
18 int samplesNeeded = floorf(totalLineLength / speed + 0.5);
|
robert@493
|
19
|
robert@493
|
20 // Now render into the buffer
|
robert@493
|
21 int length = 0;
|
robert@493
|
22 float scaleFactor = 1.0f / samplesNeeded;
|
robert@493
|
23 for(int n = 0; n < samplesNeeded; n++) {
|
robert@493
|
24 if(length >= maxLength - 1)
|
robert@493
|
25 return length;
|
robert@493
|
26 // X coordinate
|
robert@493
|
27 *buffer++ = x1 + (float)n * scaleFactor * (x2 - x1);
|
robert@493
|
28 // Y coordinate
|
robert@493
|
29 *buffer++ = y1 + (float)n * scaleFactor * (y2 - y1);
|
robert@493
|
30 length += 2;
|
robert@493
|
31 }
|
robert@493
|
32
|
robert@493
|
33 return length;
|
robert@493
|
34 }
|
robert@493
|
35
|
robert@493
|
36 // Draw an arc around a centre point at a specified rate of pixels
|
robert@493
|
37 // per buffer sample. Indicate maximum available space.
|
robert@493
|
38 // Returns space used
|
robert@493
|
39 int renderArc(float x, float y, float radius, float thetaMin, float thetaMax,
|
robert@493
|
40 float speed, float *buffer, int maxLength) {
|
robert@493
|
41 // Figure out circumference of arc and therefore how many samples
|
robert@493
|
42 // are needed to represent it based on the speed (rounded to nearest int)
|
robert@493
|
43 float circumference = (thetaMax - thetaMin) * radius;
|
robert@493
|
44 int samplesNeeded = floorf(circumference / speed + 0.5);
|
robert@493
|
45
|
robert@493
|
46 // Now render into the buffer
|
robert@493
|
47 int length = 0;
|
robert@493
|
48 float scaleFactor = 1.0f / samplesNeeded;
|
robert@493
|
49 for(int n = 0; n < samplesNeeded; n++) {
|
robert@493
|
50 if(length >= maxLength - 1)
|
robert@493
|
51 return length;
|
robert@493
|
52 // Get current angle
|
robert@493
|
53 float theta = thetaMin + (float)n * scaleFactor * (thetaMax - thetaMin);
|
robert@493
|
54
|
robert@493
|
55 // Convert polar to cartesian coordinates
|
robert@493
|
56 *buffer++ = x + radius * cosf(theta);
|
robert@493
|
57 *buffer++ = y + radius * sinf(theta);
|
robert@493
|
58
|
robert@493
|
59 length += 2;
|
robert@493
|
60 }
|
robert@493
|
61
|
robert@493
|
62 return length;
|
robert@493
|
63 }
|
robert@493
|
64
|
robert@493
|
65 // Draw a single point for a specified number of frames
|
robert@493
|
66 void renderPoint(float x, float y, float *buffer, float length) {
|
robert@493
|
67 while(length > 0) {
|
robert@493
|
68 *buffer++ = x;
|
robert@493
|
69 *buffer++ = y;
|
robert@493
|
70 length--;
|
robert@493
|
71 }
|
robert@493
|
72 }
|