Merge branch 'dev' into 'master'

Sprint - Unit Tests sind cool

See merge request link/projekte/ws19/vkvm-new/simple-draw!1
This commit is contained in:
Julian Hinxlage 2020-01-09 06:52:27 +00:00
commit bc5532cfb2
17 changed files with 904 additions and 3 deletions

32
.ci/clang-tidy.sh Normal file
View File

@ -0,0 +1,32 @@
#!/bin/sh
echo "Doing clang-tidy..."
bool=false
# explicitly set IFS to contain only a line feed
IFS='
'
filelist="$(find . -not \( -path './*build*' -prune \) -not \( -path './include' -prune \) -not \( -path './lib' -prune \) -type f ! -name "$(printf "*\n*")")"
for file in $filelist; do
if echo "$file" | grep -q -E ".*(\.cpp|\.h|\.hpp)$" ; then
#Extra check missing dependencies due to clang-tidy doesn't toggle exit code.
clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' "$file" -- -I. -std=c++14 2>&1)"
for tidy_line in $clang_tidy_lib_check; do
echo "$tidy_line" | grep -q -v -E "^Error while processing*"
if [ $? -eq 1 ]; then
bool=true
fi
echo "$tidy_line" | grep -q -v -E ".* error: .*"
if [ $? -eq 1 ]; then
bool=true
fi
echo "$tidy_line"
done
fi
done
if $bool; then
exit 1
else
echo "No clang-tidy errors found."
fi
exit 0

2
.clang-tidy Normal file
View File

@ -0,0 +1,2 @@
Checks: '*,-llvm-header-guard,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-modernize-use-trailing-return-type,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers'
WarningsAsErrors: 'true'

77
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,77 @@
---
image: samueldebruyn/debian-git:latest
stages:
- style
- test
- build
clang_tidy:
image: joethei/clang_tidy
stage: style
tags:
- docker-ci
script:
- mkdir current
- ls -d .[!.]* | grep -v current | xargs mv -t current
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.repo.digitech.hs-emden-leer.de/link/projekte/ws19/vkvm-new/library.git
- mkdir library/build
- cd library/build
- cmake ..
- make
- cd ../../current/.ci
- sh clang-tidy.sh
make_test:
stage: test
tags:
- docker-ci
script:
- apt-get update
- apt-get install -y clang make cmake clang-tidy
- mkdir current
- ls | grep -v current | xargs mv -t current
- git clone https://github.com/catchorg/Catch2.git
- cd Catch2
- cmake -Bbuild -H. -DBUILD_TESTING=OFF
- cmake --build build/ --target install
- cd ..
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.repo.digitech.hs-emden-leer.de/link/projekte/ws19/vkvm-new/library.git
- mkdir library/build
- cd library/build
- cmake ..
- make
- cd ../../current
- mkdir build
- cd build
- cmake ..
- make
- make test
cmake_build:
stage: build
tags:
- docker-ci
script:
- apt-get update
- apt-get install -y clang make cmake clang-tidy
- mkdir current
- ls | grep -v current | xargs mv -t current
- git clone https://github.com/catchorg/Catch2.git
- cd Catch2
- cmake -Bbuild -H. -DBUILD_TESTING=OFF
- cmake --build build/ --target install
- cd ..
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.repo.digitech.hs-emden-leer.de/link/projekte/ws19/vkvm-new/library.git
- mkdir library/build
- cd library/build
- cmake ..
- make
- cd ../../current
- mkdir build
- cd build
- cmake ..
- make

View File

@ -8,6 +8,9 @@ if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
endif() endif()
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "bin" "doc" "CMakeFiles" "lib" "include") set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "bin" "doc" "CMakeFiles" "lib" "include")
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
project(Simple_Draw) project(Simple_Draw)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
@ -16,7 +19,7 @@ set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=*")
set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;) set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;)
file(GLOB_RECURSE SOURCES src/*.cpp) file(GLOB_RECURSE SOURCES src/*.cpp)
file(GLOB_RECURSE HEADERS src/*.h) file(GLOB_RECURSE HEADERS src/*.hpp)
file(GLOB_RECURSE TESTS test/*.cpp) file(GLOB_RECURSE TESTS test/*.cpp)
set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library")
@ -31,6 +34,7 @@ enable_testing()
find_package(Catch2 REQUIRED) find_package(Catch2 REQUIRED)
add_executable(UnitTests ${SOURCES} ${HEADERS} ${TESTS}) add_executable(UnitTests ${SOURCES} ${HEADERS} ${TESTS})
target_link_libraries(UnitTests Catch2::Catch2) target_link_libraries(UnitTests Catch2::Catch2)
target_link_libraries(UnitTests ${LIB_PATH}/lib/liblibrary.a)
include(CTest) include(CTest)
include(Catch) include(Catch)

View File

@ -1,5 +1,114 @@
#include "../src/demo.h" #include <iostream>
#include "internal.hpp"
#include "vkvm.hpp"
#include "../src/DrawRender.hpp"
#include <unistd.h>
/**
* @author: Shaohua Tong
* @since: v0.0.1
* draw circle, square, rectangle and mouse drawing
*/
int main() { int main() {
return test(); vkvm::initialize(0);
vkvm::Color penColor(250,102,0);
int penWidth = 5;
DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), vkvm::getBackgroundColor(), penColor, penWidth);
vkvm::registerEvent(vkvm::EventType::MouseMove, [&drawRender]() {
vkvm::Coordinates mousePosition = vkvm::getMousePosition();
mousePosition.y = mousePosition.y - 30;
drawRender.setMousePostion(mousePosition);
drawRender.graphicsUpdate(CURSOR);
vkvm::callEvent(vkvm::EventType::Redraw);
if(drawRender.getMouseDown()) {
if (drawRender.getTurnOnBrush()) {
drawRender.graphicsUpdate(BRUSH);
vkvm::callEvent(vkvm::EventType::Redraw);
}
if((drawRender.getKeyCode()) == vkvm::KeyCode::C) {
drawRender.graphicsUpdate(CIRCLE);
vkvm::callEvent(vkvm::EventType::Redraw);
}
else if((drawRender.getKeyCode()) == vkvm::KeyCode::R) {
drawRender.graphicsUpdate(RECTANGLE);
vkvm::callEvent(vkvm::EventType::Redraw);
}
}
});
vkvm::registerEvent(vkvm::EventType::MouseLeftDown, [&drawRender]() {
drawRender.setFinish(false);
if(!drawRender.getMouseDown()) {
drawRender.setMouseDown(true);
vkvm::Coordinates mousePosition = vkvm::getMousePosition();
mousePosition.y = mousePosition.y - 30;
drawRender.setMouseLeftDownPostion(mousePosition);
}
if((!drawRender.getTurnOnBrush()) && ((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::B)) {
drawRender.setTurnOnBrush(true);
}
});
vkvm::registerEvent(vkvm::EventType::MouseLeftUp, [&drawRender]() {
drawRender.setFinish(true);
vkvm::Coordinates mousePosition = vkvm::getMousePosition();
mousePosition.y = mousePosition.y - 30;
drawRender.setMousePostion(mousePosition);
if(((drawRender.getKeyCode()) == vkvm::KeyCode::C) && drawRender.getPainting()) {
drawRender.graphicsUpdate(CIRCLE);
vkvm::callEvent(vkvm::EventType::Redraw);
}
else if(((drawRender.getKeyCode()) == vkvm::KeyCode::R) && drawRender.getPainting()) {
drawRender.graphicsUpdate(RECTANGLE);
vkvm::callEvent(vkvm::EventType::Redraw);
}
drawRender.setTurnOnBrush(false);
drawRender.setMouseDown(false);
drawRender.setPainting(false);
});
vkvm::registerEvent(vkvm::EventType::KeyDown, [&drawRender]() {
drawRender.setKeyCode(vkvm::getLastPressedKey());
if((drawRender.getKeyCode()) == vkvm::KeyCode::D) {
drawRender.clear();
vkvm::callEvent(vkvm::EventType::Redraw);
}
else if((drawRender.getKeyCode()) == vkvm::KeyCode::Q) {
drawRender.graphicsUpdate(SHAPE);
vkvm::callEvent(vkvm::EventType::Redraw);
}
});
vkvm::registerEvent(vkvm::EventType::KeyUp, [&drawRender]() {
drawRender.setKeyCode(vkvm::getLastPressedKey());
if((drawRender.getKeyCode()) == vkvm::KeyCode::D) {
drawRender.clear();
vkvm::callEvent(vkvm::EventType::Redraw);
}
});
while(1){
sleep(5);
}
return 0;
} }

55
src/Circle.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "Circle.hpp"
Circle::Circle() {
}
Circle::Circle(vkvm::Coordinates center, int radius, int penWidth, bool brush) {
this -> center = center;
this -> radius = radius;
int x_draw = 0;
int y_draw = 0;
int distance = 0;
uperLeft.x = center.x - radius;
uperLeft.y = center.y - radius;
bottomRight.x = center.x + radius;
bottomRight.y = center.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++) {
temp.x = uperLeft.x + x_draw;
temp.y = uperLeft.y + y_draw;
distance = utils::squareOfDistance(temp, center);
if(!brush) {
if( distance <= (radius * radius) && distance >= ((radius - penWidth) * (radius - penWidth)))
circle[y_draw][x_draw] = true;
}
else {
if( distance <= (radius * radius))
circle[y_draw][x_draw] = true;
}
}
}
}
std::vector<std::vector<bool>> Circle::getCircle() {
return circle;
}
int Circle::getRadius() {
return radius;
}
vkvm::Coordinates Circle::getUperLeft() {
return uperLeft;
}
vkvm::Coordinates Circle::getBottomRight() {
return bottomRight;
}

30
src/Circle.hpp Normal file
View File

@ -0,0 +1,30 @@
#ifndef SIMPLE_DRAW_CIRCLE_HPP
#define SIMPLE_DRAW_CIRCLE_HPP
#include "internal.hpp"
#include "vkvm.hpp"
#include "utils.hpp"
class Circle {
public:
Circle();
Circle(vkvm::Coordinates center, int radius, int penWidth, bool brush);
std::vector<std::vector<bool>> getCircle();
int getRadius();
vkvm::Coordinates getUperLeft();
vkvm::Coordinates getBottomRight();
private:
vkvm::Coordinates center;
int radius;
vkvm::Coordinates uperLeft;
vkvm::Coordinates bottomRight;
std::vector<std::vector<bool>> circle;
std::vector<std::vector<bool>> circleCreator(int penWidth, bool brush);
};
#endif //SIMPLE_DRAW_CIRCLE_H

47
src/Cursor.cpp Normal file
View File

@ -0,0 +1,47 @@
//
// Created by shaohuatong on 08.12.19.
//
#include "Cursor.hpp"
Cursor::Cursor() {
}
Cursor::Cursor(vkvm::Coordinates mousePosition, int penWidth, int radius) {
this->mousePosition = mousePosition;
this->radius = radius;
int x_draw = 0;
int y_draw = 0;
uperLeft.x = mousePosition.x - radius;
uperLeft.y = mousePosition.y - radius;
bottomRight.x = mousePosition.x + radius;
bottomRight.y = mousePosition.y + radius;
vkvm::Coordinates temp;
cursor.resize(2 * radius);
for(y_draw = 0; y_draw < 2 * radius; y_draw++) {
cursor[y_draw].resize(2 * radius);
for(x_draw = 0; x_draw < 2 * radius; x_draw++) {
if((x_draw >= radius - penWidth && x_draw <= radius + penWidth)
|| (y_draw >= radius - penWidth && y_draw <= radius + penWidth)) {
cursor[y_draw][x_draw] = true;
}
}
}
}
std::vector<std::vector<bool>> Cursor::getCursor() {
return cursor;
}
vkvm::Coordinates Cursor::getUperLeft() {
return uperLeft;
}
vkvm::Coordinates Cursor::getBottomRight() {
return bottomRight;
}

31
src/Cursor.hpp Normal file
View File

@ -0,0 +1,31 @@
//
// Created by shaohuatong on 08.12.19.
//
#ifndef SIMPLE_DRAW_CURSOR_HPP
#define SIMPLE_DRAW_CURSOR_HPP
#include "vkvm.hpp"
#include "internal.hpp"
#include "utils.hpp"
class Cursor {
public:
Cursor();
Cursor(vkvm::Coordinates mousePosition, int penWidth, int radius);
std::vector<std::vector<bool>> getCursor();
vkvm::Coordinates getUperLeft();
vkvm::Coordinates getBottomRight();
private:
int radius;
vkvm::Coordinates uperLeft;
vkvm::Coordinates bottomRight;
vkvm::Coordinates mousePosition;
std::vector<std::vector<bool>> cursor;
};
#endif //SIMPLE_DRAW_CURSOR_HPP

222
src/DrawRender.cpp Normal file
View File

@ -0,0 +1,222 @@
#include "DrawRender.hpp"
#include "vkvm.hpp"
#include <algorithm>
#include <thread>
#include <chrono>
DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth)
: backgroundColor(defaultBackgroundColor), penColor(penColor) {
this-> windowWidth = windowWidth;
this-> windowHeight = windowHeight;
this-> penWidth = penWidth;
}
void DrawRender::graphicsUpdate(int type) {
std::vector<std::vector<bool>> shape;
if (type == CIRCLE) {
if(painting)
clearToSharedMemory(oldCircle.getCircle(),
oldCircle.getUperLeft().x, oldCircle.getUperLeft().y);
int radius = utils::getDistance(getMouseLeftDownPosition(), getMousePostion()) / 2;
vkvm::Coordinates center;
center.x = (mousePosition.x + mouseLeftDownPosition.x) / 2;
center.y = (mousePosition.y + mouseLeftDownPosition.y) / 2;
if(center.x + radius > windowWidth || center.x - radius < 0
|| center.y + radius > windowHeight || center.y - radius < 0) {
radius = std::min(std::min(center.x, windowWidth - center.x), std::min(center.y, windowHeight - center.y));
}
oldCircle = Circle(center, radius, penWidth, false);
painting = true;
translateToSharedMemory(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, false);
if(finish)
shapes.addShape(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y,
oldCircle.getBottomRight().x, oldCircle.getBottomRight().y);
}
else if (type == RECTANGLE) {
if(painting)
clearToSharedMemory(oldRectangle.getRectangle(),
oldRectangle.getUperLeft().x, oldRectangle.getUperLeft().y);
oldRectangle = Rectangle(mouseLeftDownPosition, mousePosition, penWidth);
painting = true;
translateToSharedMemory(oldRectangle.getRectangle(),
oldRectangle.getUperLeft().x, oldRectangle.getUperLeft().y, false);
if(finish)
shapes.addShape(oldRectangle.getRectangle(), oldRectangle.getUperLeft().x, oldRectangle.getUperLeft().y,
oldRectangle.getBottomRight().x, oldRectangle.getBottomRight().y);
}
else if (type == BRUSH) {
int radius = penWidth;
vkvm::Coordinates center = mousePosition;
oldCircle = Circle(center, radius, penWidth, true);
translateToSharedMemory(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, false);
if(finish)
shapes.addShape(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y,
oldCircle.getBottomRight().x, oldCircle.getBottomRight().y);
}
else if (type == CURSOR) {
if(cursorStart != 0)
clearToSharedMemory(oldCursor.getCursor(),
oldCursor.getUperLeft().x, oldCursor.getUperLeft().y);
int radius = 10;
oldCursor = Cursor(mousePosition, 1, radius);
cursorStart = 1;
translateToSharedMemory(oldCursor.getCursor(), oldCursor.getUperLeft().x, oldCursor.getUperLeft().y, true);
}
else if (type == SHAPE) {
for(int i = 0; i < shapes.getCount(); i++) {
penColor = vkvm::Color(rand() % 255,rand() % 255,rand() % 255);
translateToSharedMemory(shapes.getShape(i), shapes.getStartX(i), shapes.getStartY(i), false);
}
}
}
void DrawRender::clear() {
int x, y;
for(y = 0; y < windowHeight; y++) {
for(x = 0; x < windowWidth; x++) {
vkvm::setPixel(x, y, backgroundColor);
}
}
shapes = Shapes();
}
void DrawRender::cursorCreator() {
}
void DrawRender::translateToSharedMemory(std::vector<std::vector<bool>> shape, int startX, int startY, bool isCursor) {
int x, y;
int currentX = startX;
int currentY = startY;
for(y = 0; y < shape.size(); y++) {
for(x = 0; x < shape[y].size(); x++) {
if(shape[y][x]) {
if(isCursor)
vkvm::setPixel(currentX, currentY, vkvm::Color(0,238,0));
else if(turnOnBrush || finish)
vkvm::setPixel(currentX, currentY, penColor);
else
vkvm::setPixel(currentX, currentY, vkvm::Color(217,217,217));
}
currentX++;
}
currentX = startX;
currentY++;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
void DrawRender::clearToSharedMemory(std::vector<std::vector<bool>> shape, int startX, int startY) {
int x, y;
int currentX = startX;
int currentY = startY;
for(y = 0; y < shape.size(); y++) {
for(x = 0; x < shape[y].size(); x++) {
if((shape[y][x])) {
if(!shapes.containsPixel(x + startX, y + startY)) {
vkvm::setPixel(currentX, currentY, backgroundColor);
}
else {
vkvm::setPixel(currentX, currentY, penColor);
}
}
currentX++;
}
currentX = startX;
currentY++;
}
}
vkvm::Coordinates DrawRender::getMouseLeftDownPosition() {
return mouseLeftDownPosition;
}
void DrawRender::setMouseLeftDownPostion(vkvm::Coordinates newMousePosition) {
mouseLeftDownPosition = newMousePosition;
}
vkvm::Coordinates DrawRender::getMousePostion() {
return mousePosition;
}
void DrawRender::setMousePostion(vkvm::Coordinates newMousePosition) {
if(newMousePosition.x >= 0 && newMousePosition.x <= windowWidth && newMousePosition.y >= 30 && newMousePosition.y <= windowHeight) {
}
else {
newMousePosition.x = newMousePosition.x < 0 ? 0: newMousePosition.x;
newMousePosition.y = newMousePosition.y < 30 ? 0 : newMousePosition.y;
newMousePosition.x = newMousePosition.x > windowWidth ? windowWidth : newMousePosition.x;
newMousePosition.y = newMousePosition.y > windowHeight ? windowHeight : newMousePosition.y;
}
mousePosition = newMousePosition;
}
void DrawRender::setKeyCode(vkvm::KeyCode newKeyCode) {
keyCode = newKeyCode;
}
vkvm::KeyCode DrawRender::getKeyCode() {
return keyCode;
}
void DrawRender::setTurnOnBrush(bool newTurnOnBrush) {
turnOnBrush = newTurnOnBrush;
}
bool DrawRender::getTurnOnBrush() {
return turnOnBrush;
}
bool DrawRender::getMouseDown() {
return mouseDown;
}
void DrawRender::setMouseDown(bool isMouseDown) {
mouseDown = isMouseDown;
}
bool DrawRender::getPainting() {
return painting;
}
void DrawRender::setPainting(bool isPainting) {
painting = isPainting;
}
void DrawRender::setFinish(bool isFinish) {
finish = isFinish;
}
bool DrawRender::getFinish() {
return finish;
}

85
src/DrawRender.hpp Normal file
View File

@ -0,0 +1,85 @@
//
// Created by shaohuatong on 21.11.19.
//
#ifndef SIMPLE_DRAW_DRAWRENDER_HPP
#define SIMPLE_DRAW_DRAWRENDER_HPP
#include "Cursor.hpp"
#include "Rectangle.hpp"
#include "Circle.hpp"
#include "Shapes.hpp"
#include "utils.hpp"
#include "vkvm.hpp"
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#define CIRCLE 0
#define RECTANGLE 1
#define BRUSH 2
#define CURSOR 3
#define SHAPE 4
class DrawRender {
public:
DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth);
void graphicsUpdate(int type);
void setMouseLeftDownPostion(vkvm::Coordinates newMousePosition);
vkvm::Coordinates getMouseLeftDownPosition();
void setMousePostion(vkvm::Coordinates newMousePosition);
vkvm::Coordinates getMousePostion();
void setKeyCode(vkvm::KeyCode newKeyCode);
vkvm::KeyCode getKeyCode();
void setTurnOnBrush(bool turnOnBrush);
bool getTurnOnBrush();
void setMouseDown(bool isMouseDown);
bool getMouseDown();
void setPainting(bool isOneFinish);
bool getPainting();
void setFinish(bool isFinish);
bool getFinish();
void clear();
private:
Shapes shapes;
Circle oldCircle = Circle();
Rectangle oldRectangle = Rectangle();
Cursor oldCursor = Cursor();
vkvm::Coordinates mouseLeftDownPosition;
vkvm::Coordinates mousePosition;
vkvm::Color backgroundColor;
vkvm::Color penColor;
vkvm::KeyCode keyCode;
int penWidth;
int windowWidth;
int windowHeight;
int cursorStart;
bool painting = true;
bool finish = false;
bool mouseDown = false;
bool turnOnBrush = false;
void cursorCreator();
void translateToSharedMemory(std::vector<std::vector<bool>> graphic, int startX, int startY, bool isCursor);
void clearToSharedMemory(std::vector<std::vector<bool>> graphic, int startX, int startY);
};
#endif //SIMPLE_DRAW_DRAWRENDER_HPP

57
src/Rectangle.cpp Normal file
View File

@ -0,0 +1,57 @@
//
// Created by shaohuatong on 06.12.19.
//
#include "Rectangle.hpp"
Rectangle::Rectangle() {
}
Rectangle::Rectangle(vkvm::Coordinates mouseLeftDownPostion, vkvm::Coordinates mousePostion, int penWidth) {
this -> mouseLeftDownPosition = mouseLeftDownPostion;
this -> mousePosition = mousePostion;
int x_draw = 0;
int y_draw = 0;
length = abs(mouseLeftDownPosition.x - mousePosition.x);
width = abs(mouseLeftDownPosition.y - mousePosition.y);
uperLeft.x = std::min(mouseLeftDownPosition.x, mousePosition.x);
uperLeft.y = std::min(mouseLeftDownPosition.y, mousePosition.y);
bottomRight.x = std::max(mouseLeftDownPosition.x, mousePosition.x);
bottomRight.y = std::max(mouseLeftDownPosition.y, mousePosition.y);
rectangle.resize(width);
for(y_draw = 0; y_draw < width; y_draw++) {
rectangle[y_draw].resize(length);
for(x_draw = 0; x_draw < length; x_draw++) {
if((x_draw >= 0 && x_draw <= penWidth) || (y_draw >= 0 && y_draw <= penWidth)
|| (x_draw <= length && x_draw >= length - penWidth)
|| (y_draw <= width && y_draw >= width - penWidth)) {
rectangle[y_draw][x_draw] = true;
}
}
}
}
std::vector<std::vector<bool>> Rectangle::getRectangle() {
return rectangle;
}
int Rectangle::getWidth() {
return width;
}
int Rectangle::getLength() {
return length;
}
vkvm::Coordinates Rectangle::getUperLeft() {
return uperLeft;
}
vkvm::Coordinates Rectangle::getBottomRight() {
return bottomRight;
}

34
src/Rectangle.hpp Normal file
View File

@ -0,0 +1,34 @@
//
// Created by shaohuatong on 06.12.19.
//
#ifndef SIMPLE_DRAW_RECTANGLE_HPP
#define SIMPLE_DRAW_RECTANGLE_HPP
#include "internal.hpp"
#include "vkvm.hpp"
#include "utils.hpp"
class Rectangle {
public:
Rectangle();
Rectangle(vkvm::Coordinates mouseLeftDownPosition, vkvm::Coordinates mousePosition, int penWidth);
std::vector<std::vector<bool>> getRectangle();
int getLength();
int getWidth();
vkvm::Coordinates getUperLeft();
vkvm::Coordinates getBottomRight();
private:
int length;
int width;
vkvm::Coordinates uperLeft;
vkvm::Coordinates bottomRight;
vkvm::Coordinates mouseLeftDownPosition;
vkvm::Coordinates mousePosition;
std::vector<std::vector<bool>> rectangle;
};
#endif //SIMPLE_DRAW_RECTANGLE_HPP

58
src/Shapes.cpp Normal file
View File

@ -0,0 +1,58 @@
//
// Created by shaohuatong on 06.12.19.
//
#include <iostream>
#include "Shapes.hpp"
void Shapes::addShape(std::vector<std::vector<bool>> shape, int startX, int startY, int endX, int endY) {
count++;
shapes.resize(count);
startXs.resize(count);
startYs.resize(count);
endXs.resize(count);
endYs.resize(count);
startXs[count-1] = startX;
startYs[count-1] = startY;
endXs[count-1] = endX;
endYs[count-1] = endY;
// shapes[count-1].resize(shape.size());
// for(int y = 0; y < shape.size(); y++) {
// shapes[count-1][y].resize(shape[y].size());
// for(int x = 0; x < shape[y].size(); x++) {
// shapes[count-1][y][x] = shape[y][x];
// }
// }
shapes[count-1] = shape;
}
bool Shapes::containsPixel(int x, int y) {
for(int i = 0; i < count; i++) {
if(x >= startXs[i] && x < endXs[i] && y >= startYs[i] && y < endYs[i]) {
if(shapes[i][y-startYs[i]][x-startXs[i]]) {
return true;
}
}
}
return false;
}
int Shapes::getCount() {
return count;
}
std::vector<std::vector<bool>> Shapes::getShape(int index) {
return shapes[index];
}
int Shapes::getStartX(int index) {
return startXs[index];
}
int Shapes::getStartY(int index) {
return startYs[index];
}

31
src/Shapes.hpp Normal file
View File

@ -0,0 +1,31 @@
//
// Created by shaohuatong on 06.12.19.
//
#ifndef SIMPLE_DRAW_SHAPES_HPP
#define SIMPLE_DRAW_SHAPES_HPP
#include "vkvm.hpp"
#include "internal.hpp"
class Shapes {
public:
void addShape(std::vector<std::vector<bool>> shape, int startX,
int startY, int endX, int endY);
bool containsPixel(int x, int y);
int getCount();
std::vector<std::vector<bool>> getShape(int index);
int getStartX(int index);
int getStartY(int index);
private:
int count = 0;
std::vector<int> startXs;
std::vector<int> startYs;
std::vector<int> endXs;
std::vector<int> endYs;
std::vector<std::vector<std::vector<bool>>> shapes;
};
#endif //SIMPLE_DRAW_SHAPES_HPP

9
src/utils.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "utils.hpp"
int utils::squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y) {
return (x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y);
}
int utils::getDistance(vkvm::Coordinates x, vkvm::Coordinates y) {
return (int)floor(sqrt(squareOfDistance(x, y)));
}

18
src/utils.hpp Normal file
View File

@ -0,0 +1,18 @@
//
// Created by shaohuatong on 06.12.19.
//
#ifndef SIMPLE_DRAW_UTILS_HPP
#define SIMPLE_DRAW_UTILS_HPP
#include "vkvm.hpp"
#include <cmath>
class utils {
public:
static int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y);
static int getDistance(vkvm::Coordinates x, vkvm::Coordinates y);
};
#endif //SIMPLE_DRAW_UTILS_H