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