164 lines
4.4 KiB
C++
164 lines
4.4 KiB
C++
//
|
|
// Created by shaohuatong on 21.11.19.
|
|
//
|
|
|
|
#include "DrawRender.hpp"
|
|
#include <algorithm>
|
|
|
|
DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color penColor, int penWidth)
|
|
: backgroundColor(backgroundColor), penColor(penColor) {
|
|
this-> windowWidth = windowWidth;
|
|
this-> windowHeight = windowHeight;
|
|
this-> penWidth = penWidth;
|
|
}
|
|
|
|
void DrawRender::update(vkvm::Coordinates mousePostion, int type) {
|
|
if(type == BRUSH) {
|
|
std::vector<std::vector<bool>> circleBrush = brushCreator(mousePostion);
|
|
translateToSharedMemory(circleBrush, startX, startY, type);
|
|
|
|
}
|
|
else if(type == CIRCLE || type == SQUARE) {
|
|
std::vector<std::vector<bool>> graphics = circleAndSquareCreator(mousePostion, type);
|
|
translateToSharedMemory(graphics, startX, startY, type);
|
|
}
|
|
}
|
|
|
|
void DrawRender::clear() {
|
|
int x, y;
|
|
for(y = 0; y < windowHeight; y++) {
|
|
for(x = 0; x < windowWidth; x++) {
|
|
vkvm::setPixel(x, y, backgroundColor);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<std::vector<bool>> DrawRender::circleAndSquareCreator(vkvm::Coordinates mousePosition, int type) {
|
|
std::vector<std::vector<bool>> circle;
|
|
int x_draw = 0;
|
|
int y_draw = 0;
|
|
int distance = 0;
|
|
radius = std::min(std::min(mousePosition.x, mousePosition.y),
|
|
std::min(windowWidth - mousePosition.x, windowHeight - mousePosition.y)) * 0.8;
|
|
vkvm::Coordinates uperLeft;
|
|
vkvm::Coordinates bottomRight;
|
|
uperLeft.x = mousePosition.x - radius;
|
|
uperLeft.y = mousePosition.y - radius;
|
|
bottomRight.x = mousePosition.x + radius;
|
|
bottomRight.y = mousePosition.y + radius;
|
|
|
|
vkvm::Coordinates temp;
|
|
circle.resize(2 * radius);
|
|
for(y_draw = 0; y_draw < 2 * radius; y_draw++) {
|
|
circle[y_draw].resize(2 * radius);
|
|
for(x_draw = 0; x_draw < 2 * radius; x_draw++) {
|
|
if(type == CIRCLE) {
|
|
temp.x = uperLeft.x + x_draw;
|
|
temp.y = uperLeft.y + y_draw;
|
|
distance = squareOfDistance(temp, mousePosition);
|
|
|
|
if( distance < (radius * radius) && distance > ((radius - penWidth) * (radius - penWidth))) {
|
|
circle[y_draw][x_draw] = true;
|
|
}
|
|
}
|
|
if(type == SQUARE) {
|
|
if((x_draw >= 0 && x_draw <= penWidth) || (y_draw >= 0 && y_draw <= penWidth)
|
|
|| (x_draw <= 2 * radius && x_draw >= 2 * radius - penWidth)
|
|
|| (y_draw <= 2 * radius && y_draw >= 2 * radius - penWidth)) {
|
|
circle[y_draw][x_draw] = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
startX = uperLeft.x;
|
|
startY = uperLeft.y;
|
|
return circle;
|
|
}
|
|
|
|
std::vector<std::vector<bool>> DrawRender::brushCreator(vkvm::Coordinates mousePosition) {
|
|
std::vector<std::vector<bool>> circleBrush;
|
|
int x_draw = 0;
|
|
int y_draw = 0;
|
|
int distance = 0;
|
|
radius = penWidth;
|
|
vkvm::Coordinates uperLeft;
|
|
vkvm::Coordinates bottomRight;
|
|
uperLeft.x = mousePosition.x - radius;
|
|
uperLeft.y = mousePosition.y - radius;
|
|
bottomRight.x = mousePosition.x + radius;
|
|
bottomRight.y = mousePosition.y + radius;
|
|
|
|
vkvm::Coordinates temp;
|
|
circleBrush.resize(2 * radius);
|
|
for(y_draw = 0; y_draw < 2 * radius; y_draw++) {
|
|
circleBrush[y_draw].resize(2 * radius);
|
|
for(x_draw = 0; x_draw < 2 * radius; x_draw++) {
|
|
temp.x = uperLeft.x + x_draw;
|
|
temp.y = uperLeft.y + y_draw;
|
|
distance = squareOfDistance(temp, mousePosition);
|
|
if( distance < (radius * radius)) {
|
|
circleBrush[y_draw][x_draw] = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
startX = uperLeft.x;
|
|
startY = uperLeft.y;
|
|
return circleBrush;
|
|
}
|
|
|
|
|
|
void DrawRender::translateToSharedMemory(std::vector<std::vector<bool>> graphics, int startX, int startY, int type) {
|
|
int x, y;
|
|
int currentX = startX;
|
|
int currentY = startY;
|
|
if(type == CIRCLE || type == SQUARE || type == BRUSH) {
|
|
for(y = 0; y < 2 * radius; y++) {
|
|
for(x = 0; x < 2 * radius; x++) {
|
|
if(graphics[y][x]) {
|
|
vkvm::setPixel(currentX, currentY, penColor);
|
|
}
|
|
currentX++;
|
|
}
|
|
currentX = startX;
|
|
currentY++;
|
|
}
|
|
}
|
|
}
|
|
|
|
int DrawRender::squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y) {
|
|
return (x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|