unlogic
Loading...
Searching...
No Matches
Plot.cpp
Go to the documentation of this file.
1//
2// Created by Nathan on 11/14/2024.
3//
4#include <numbers>
5#include <vector>
6#include "Plot.h"
7
8using namespace unlogic;
9
10Plot2d::Plot2d(std::string name, Plot2dFunctionType fn, Color color) : name_(std::move(name)), fn_(fn), color_(color)
11{
12 std::size_t point_count = std::ceil((this->domain_.y - this->domain_.x) / this->precision_);
13
14 std::vector<glm::vec2> points;
15 points.reserve(point_count);
16 for (std::size_t i = 0; i < point_count; i++)
17 {
18 double x = this->domain_.x + ((double)i * this->precision_);
19 double y = this->fn_(x);
20
21 points.emplace_back(x, y);
22 }
23
24 this->vertices.clear();
25 this->vertices.reserve(points.size() * 2);
26 double dx = 0.0;
27 double dy = 0.0;
28 for (std::size_t i = 0; i < points.size(); i++)
29 {
30 if (i < points.size() - 1)
31 {
32 dx = points[i + 1].x - points[i].x;
33 dy = points[i + 1].y - points[i].y;
34 }
35
36 double theta1 = std::atan2(dy, dx);
37 double theta2 = ((std::numbers::pi / 2) - theta1);
38
39 double tx = (this->line_thickness_ / 2) * std::cos(theta2);
40 double ty = (this->line_thickness_ / 2) * std::sin(theta2);
41
42 auto const &point = points[i];
43
44 this->vertices.push_back({glm::vec2(point.x + tx, point.y - ty), this->color_}); // a1
45 this->vertices.push_back({glm::vec2(point.x - tx, point.y + ty), this->color_}); // a2
46 }
47}
Plot2d(std::string name, Plot2dFunctionType fn, Color color)
Definition Plot.cpp:10
std::vector< Vertex > vertices
Definition Shape.h:14
double(*)(double) Plot2dFunctionType
Definition Plot.h:15