andrewm@10: /* andrewm@10: * vector_graphics.cpp andrewm@10: * andrewm@10: * Created on: Nov 10, 2014 andrewm@10: * Author: parallels andrewm@10: */ andrewm@10: andrewm@10: #include andrewm@10: andrewm@10: // Draw a line between two points at a specified rate in andrewm@10: // pixels per buffer sample. Indicate maximum available space. andrewm@10: // Returns space used andrewm@10: int renderLine(float x1, float y1, float x2, float y2, float speed, andrewm@10: float *buffer, int maxLength) { andrewm@10: // Figure out length of line and therefore how many samples andrewm@10: // are needed to represent it based on the speed (rounded to nearest int) andrewm@10: float totalLineLength = sqrtf((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)); andrewm@10: int samplesNeeded = floorf(totalLineLength / speed + 0.5); andrewm@10: andrewm@10: // Now render into the buffer andrewm@10: int length = 0; andrewm@10: float scaleFactor = 1.0f / samplesNeeded; andrewm@10: for(int n = 0; n < samplesNeeded; n++) { andrewm@10: if(length >= maxLength - 1) andrewm@10: return length; andrewm@10: // X coordinate andrewm@10: *buffer++ = x1 + (float)n * scaleFactor * (x2 - x1); andrewm@10: // Y coordinate andrewm@10: *buffer++ = y1 + (float)n * scaleFactor * (y2 - y1); andrewm@10: length += 2; andrewm@10: } andrewm@10: andrewm@10: return length; andrewm@10: } andrewm@10: andrewm@10: // Draw an arc around a centre point at a specified rate of pixels andrewm@10: // per buffer sample. Indicate maximum available space. andrewm@10: // Returns space used andrewm@10: int renderArc(float x, float y, float radius, float thetaMin, float thetaMax, andrewm@10: float speed, float *buffer, int maxLength) { andrewm@10: // Figure out circumference of arc and therefore how many samples andrewm@10: // are needed to represent it based on the speed (rounded to nearest int) andrewm@10: float circumference = (thetaMax - thetaMin) * radius; andrewm@10: int samplesNeeded = floorf(circumference / speed + 0.5); andrewm@10: andrewm@10: // Now render into the buffer andrewm@10: int length = 0; andrewm@10: float scaleFactor = 1.0f / samplesNeeded; andrewm@10: for(int n = 0; n < samplesNeeded; n++) { andrewm@10: if(length >= maxLength - 1) andrewm@10: return length; andrewm@10: // Get current angle andrewm@10: float theta = thetaMin + (float)n * scaleFactor * (thetaMax - thetaMin); andrewm@10: andrewm@10: // Convert polar to cartesian coordinates andrewm@10: *buffer++ = x + radius * cosf(theta); andrewm@10: *buffer++ = y + radius * sinf(theta); andrewm@10: andrewm@10: length += 2; andrewm@10: } andrewm@10: andrewm@10: return length; andrewm@10: } andrewm@10: andrewm@10: // Draw a single point for a specified number of frames andrewm@10: void renderPoint(float x, float y, float *buffer, float length) { andrewm@10: while(length > 0) { andrewm@10: *buffer++ = x; andrewm@10: *buffer++ = y; andrewm@10: length--; andrewm@10: } andrewm@10: }