osci-render/Source/shape/Shape.cpp

127 wiersze
3.6 KiB
C++
Czysty Zwykły widok Historia

#include "Shape.h"
2023-02-05 17:39:02 +00:00
#include "Line.h"
2024-01-07 16:17:20 +00:00
#include "Point.h"
double Shape::totalLength(std::vector<std::unique_ptr<Shape>>& shapes) {
double length = 0.0;
for (auto& shape : shapes) {
length += shape->length();
}
return length;
}
2023-02-05 17:39:02 +00:00
void Shape::normalize(std::vector<std::unique_ptr<Shape>>& shapes, double width, double height) {
double maxDim = std::max(width, height);
for (auto& shape : shapes) {
shape->scale(2.0 / maxDim, -2.0 / maxDim, 2.0 / maxDim);
shape->translate(-1.0, 1.0, 0.0);
2023-02-05 17:39:02 +00:00
}
removeOutOfBounds(shapes);
}
void Shape::normalize(std::vector<std::unique_ptr<Shape>>& shapes) {
double oldHeight = height(shapes);
double oldWidth = width(shapes);
double maxDim = std::max(oldHeight, oldWidth);
for (auto& shape : shapes) {
shape->scale(2.0 / maxDim, -2.0 / maxDim, 2.0 / maxDim);
2023-02-05 17:39:02 +00:00
}
2024-01-07 16:17:20 +00:00
Point max = maxVector(shapes);
2023-02-05 17:39:02 +00:00
double newHeight = height(shapes);
for (auto& shape : shapes) {
shape->translate(-1.0, -max.y + newHeight / 2.0, 0.0);
2023-02-05 17:39:02 +00:00
}
}
double Shape::height(std::vector<std::unique_ptr<Shape>>& shapes) {
double maxY = std::numeric_limits<double>::min();
double minY = std::numeric_limits<double>::max();
2024-01-07 16:17:20 +00:00
Point vectors[4];
2023-02-05 17:39:02 +00:00
for (auto& shape : shapes) {
for (int i = 0; i < 4; i++) {
vectors[i] = shape->nextVector(i * 1.0 / 4.0);
}
for (auto& vector : vectors) {
maxY = std::max(maxY, vector.y);
minY = std::min(minY, vector.y);
}
}
return std::abs(maxY - minY);
}
double Shape::width(std::vector<std::unique_ptr<Shape>>& shapes) {
double maxX = std::numeric_limits<double>::min();
double minX = std::numeric_limits<double>::max();
2024-01-07 16:17:20 +00:00
Point vectors[4];
2023-02-05 17:39:02 +00:00
for (auto& shape : shapes) {
for (int i = 0; i < 4; i++) {
vectors[i] = shape->nextVector(i * 1.0 / 4.0);
}
for (auto& vector : vectors) {
maxX = std::max(maxX, vector.x);
minX = std::min(minX, vector.x);
}
}
return std::abs(maxX - minX);
}
2024-01-07 16:17:20 +00:00
Point Shape::maxVector(std::vector<std::unique_ptr<Shape>>& shapes) {
2023-02-05 17:39:02 +00:00
double maxX = std::numeric_limits<double>::min();
double maxY = std::numeric_limits<double>::min();
for (auto& shape : shapes) {
2024-01-07 16:17:20 +00:00
Point startVector = shape->nextVector(0);
Point endVector = shape->nextVector(1);
2023-02-05 17:39:02 +00:00
double x = std::max(startVector.x, endVector.x);
double y = std::max(startVector.y, endVector.y);
maxX = std::max(x, maxX);
maxY = std::max(y, maxY);
}
2024-01-07 16:17:20 +00:00
return Point(maxX, maxY);
2023-02-05 17:39:02 +00:00
}
void Shape::removeOutOfBounds(std::vector<std::unique_ptr<Shape>>& shapes) {
std::vector<int> toRemove;
2023-02-05 17:39:02 +00:00
for (int i = 0; i < shapes.size(); i++) {
2024-01-07 16:17:20 +00:00
Point start = shapes[i]->nextVector(0);
Point end = shapes[i]->nextVector(1);
bool keep = false;
2023-02-05 17:39:02 +00:00
if ((start.x < 1 && start.x > -1) || (start.y < 1 && start.y > -1)) {
if ((end.x < 1 && end.x > -1) || (end.y < 1 && end.y > -1)) {
if (shapes[i]->type() == "Line") {
2024-01-07 16:17:20 +00:00
Point newStart(std::min(std::max(start.x, -1.0), 1.0), std::min(std::max(start.y, -1.0), 1.0));
Point newEnd(std::min(std::max(end.x, -1.0), 1.0), std::min(std::max(end.y, -1.0), 1.0));
2023-02-05 17:39:02 +00:00
shapes[i] = std::make_unique<Line>(newStart.x, newStart.y, newEnd.x, newEnd.y);
}
keep = true;
2023-02-05 17:39:02 +00:00
}
}
if (!keep) {
toRemove.push_back(i);
}
}
for (int i = toRemove.size() - 1; i >= 0; i--) {
shapes.erase(shapes.begin() + toRemove[i]);
2023-02-05 17:39:02 +00:00
}
}