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