|
|
@ -16,6 +16,7 @@ |
|
|
|
#include <stdio.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
using namespace std; |
|
|
|
|
|
|
@ -30,7 +31,7 @@ Rester::~Rester() { |
|
|
|
delete buffer; |
|
|
|
} |
|
|
|
|
|
|
|
void Rester::drawPointer(int x, int y, Rgba color, int pointSize) { |
|
|
|
void Rester::drawPoint(int x, int y, Rgba color, int pointSize) { |
|
|
|
switch (pointSize) { |
|
|
|
case 1: |
|
|
|
setPixel(x, y, color); |
|
|
@ -54,6 +55,143 @@ void Rester::drawPointer(int x, int y, Rgba color, int pointSize) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void Rester::drawLine(float2 p1, float2 p2, Rgba color) { |
|
|
|
|
|
|
|
float xOffer = p1.x - p2.x; |
|
|
|
float yOffer = p1.y - p2.y; |
|
|
|
float xStart, xEnd, yStart, yEnd; |
|
|
|
|
|
|
|
if (xOffer == 0 && yOffer == 0) { |
|
|
|
setPixel(p1.x, p1.y, color); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (fabs(xOffer) > fabs(yOffer)) { |
|
|
|
if (p1.x < p2.x) { |
|
|
|
xStart = p1.x; |
|
|
|
xEnd = p2.x; |
|
|
|
yEnd = p2.y; |
|
|
|
} else { |
|
|
|
xStart = p2.x; |
|
|
|
xEnd = p1.x; |
|
|
|
yEnd = p1.y; |
|
|
|
} |
|
|
|
|
|
|
|
float slope = yOffer / xOffer; |
|
|
|
for (float i = xStart; i < xEnd; i += 1.0) { |
|
|
|
float q = yEnd - slope * (xEnd - i); |
|
|
|
setPixel(i, q, color); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (p1.y < p2.y) { |
|
|
|
yStart = p1.y; |
|
|
|
yEnd = p2.y; |
|
|
|
xEnd = p2.x; |
|
|
|
} else { |
|
|
|
yStart = p2.y; |
|
|
|
yEnd = p1.y; |
|
|
|
xEnd = p1.x; |
|
|
|
} |
|
|
|
float slope = xOffer / yOffer; |
|
|
|
for (float i = yStart; i < yEnd; i += 1.0) { |
|
|
|
float q = xEnd - slope * (yEnd - i); |
|
|
|
setPixel(q, i, color); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void Rester::drawLine(float2 p1, float2 p2, Rgba color1, Rgba color2) { |
|
|
|
|
|
|
|
float xOffer = p1.x - p2.x; |
|
|
|
float yOffer = p1.y - p2.y; |
|
|
|
float xStart, xEnd, yStart, yEnd, step; |
|
|
|
|
|
|
|
if (xOffer == 0 && yOffer == 0) { |
|
|
|
setPixel(p1.x, p1.y, color1); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (fabs(xOffer) > fabs(yOffer)) { |
|
|
|
if (p1.x < p2.x) { |
|
|
|
xStart = p1.x; |
|
|
|
xEnd = p2.x; |
|
|
|
yEnd = p2.y; |
|
|
|
} else { |
|
|
|
xStart = p2.x; |
|
|
|
xEnd = p1.x; |
|
|
|
yEnd = p1.y; |
|
|
|
} |
|
|
|
|
|
|
|
float slope = yOffer / xOffer; |
|
|
|
float length = xEnd - xStart; |
|
|
|
|
|
|
|
for (float i = xStart; i < xEnd; i += 1.0) { |
|
|
|
float q = yEnd - slope * (xEnd - i); |
|
|
|
step = (i - xStart) / length; |
|
|
|
Rgba color = colorLerp(color1, color2, step); |
|
|
|
setPixel(i, q, color); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (p1.y < p2.y) { |
|
|
|
yStart = p1.y; |
|
|
|
yEnd = p2.y; |
|
|
|
xEnd = p2.x; |
|
|
|
} else { |
|
|
|
yStart = p2.y; |
|
|
|
yEnd = p1.y; |
|
|
|
xEnd = p1.x; |
|
|
|
} |
|
|
|
float slope = xOffer / yOffer; |
|
|
|
float length = yEnd - yStart; |
|
|
|
for (float i = yStart; i < yEnd; i += 1.0) { |
|
|
|
float q = xEnd - slope * (yEnd - i); |
|
|
|
step = (i - yStart) / length; |
|
|
|
Rgba color = colorLerp(color1, color2, step); |
|
|
|
setPixel(q, i, color); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void Rester::drawArray(DRAWMODE mode, const float2* points, size_t count) { |
|
|
|
switch (mode) { |
|
|
|
case DM_POINTS: |
|
|
|
{ |
|
|
|
for (size_t i = 0; i < count; i++) { |
|
|
|
drawPoint(points[0].x, points[1].y); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
case DM_LINES: |
|
|
|
{ |
|
|
|
count = count / 2 * 2; //保证其为2的倍数
|
|
|
|
for (size_t i = 0; i < count; i++) { |
|
|
|
drawLine(points[i], points[i + 1]); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
case DM_LINES_LOOP: |
|
|
|
{ |
|
|
|
size_t i = count; |
|
|
|
drawLine(points[0], points[1]); |
|
|
|
for (i = 2; i < count; i++) { |
|
|
|
drawLine(points[i - 1], points[i]); |
|
|
|
} |
|
|
|
drawLine(points[0], points[i]); |
|
|
|
} |
|
|
|
break; |
|
|
|
case DM_LINES_TRIP: |
|
|
|
{ |
|
|
|
drawLine(points[0], points[1]); |
|
|
|
for (size_t i = 2; i < count; i++) { |
|
|
|
drawLine(points[i - 1], points[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
int Rester::size() { |
|
|
|
return _width * _height * sizeof (Rgba); |
|
|
|
} |
|
|
@ -63,9 +201,9 @@ void Rester::clean() { |
|
|
|
} |
|
|
|
|
|
|
|
bool Rester::setPixel(int x, int y, Rgba color) { |
|
|
|
if (x < 0 || y < 0 || x >= 500 || y >= 500) { |
|
|
|
if (x < 0 || y < 0 || x >= _width || y >= _height) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
//行列是反的
|
|
|
|
buffer[y * _width + x] = color; |
|
|
|
buffer[ (_height - y) * _width + x] = color; |
|
|
|
} |