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