annotate examples/10-Instruments/tank_wars/vector_graphics.cpp @ 492:e9821d65b9ba prerelease

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