Coverage report for gui


src/
File: src/render/scenes/world/shape/diamond.hpp
Date: 2024-06-25 10:57:02
Lines:
0/30
0.0%
Functions:
0/5
0.0%
Branches:
0/54
0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** diamond
6 */
7
8 #ifndef DIAMOND_HPP_
9 #define DIAMOND_HPP_
10
11 #include <SFML/Graphics.hpp>
12
13 class Diamond {
14 public:
15 Diamond() {};
16 Diamond(sf::Vector2f tileSize) {
17 _tileSize = tileSize;
18 _triangle1.setPointCount(3);
19 _triangle1.setPoint(0, sf::Vector2f(_tileSize.x / 2, 0));
20 _triangle1.setPoint(1, sf::Vector2f(_tileSize.x, _tileSize.y / 4 + 1));
21 _triangle1.setPoint(2, sf::Vector2f(_tileSize.x / 2, _tileSize.y / 2 + 1));
22 _triangle1.setFillColor(sf::Color::Green);
23 _triangle2.setPointCount(3);
24 _triangle2.setPoint(0, sf::Vector2f(_tileSize.x / 2, 0));
25 _triangle2.setPoint(1, sf::Vector2f(0, _tileSize.y / 4 + 1));
26 _triangle2.setPoint(2, sf::Vector2f(_tileSize.x / 2, _tileSize.y / 2 + 1));
27 _triangle2.setFillColor(sf::Color::Blue);
28 }
29 ~Diamond() {};
30
31 void draw(sf::RenderWindow &window) {
32 window.draw(_triangle1);
33 window.draw(_triangle2);
34 }
35
36 float crossProduct(sf::Vector2f a, sf::Vector2f b) {
37 return (a.x * b.y - a.y * b.x);
38 }
39
40 bool checkTriangle(sf::ConvexShape triangle, const sf::Vector2f& mousePos) {
41 sf::Vector2f v1 = triangle.getPoint(0) + _pos;
42 sf::Vector2f v2 = triangle.getPoint(1) + _pos;
43 sf::Vector2f v3 = triangle.getPoint(2) + _pos;
44
45 sf::Vector2f d1 = v2 - v1;
46 sf::Vector2f d2 = v3 - v2;
47 sf::Vector2f d3 = v1 - v3;
48
49 sf::Vector2f c1 = mousePos - v1;
50 sf::Vector2f c2 = mousePos - v2;
51 sf::Vector2f c3 = mousePos - v3;
52
53 float cross1 = crossProduct(d1, c1);
54 float cross2 = crossProduct(d2, c2);
55 float cross3 = crossProduct(d3, c3);
56
57 bool hasNeg = (cross1 < 0) || (cross2 < 0) || (cross3 < 0);
58 bool hasPos = (cross1 > 0) || (cross2 > 0) || (cross3 > 0);
59
60 return !(hasNeg && hasPos);
61 }
62
63 bool checkCollision(sf::Vector2f mousePos) {
64 if (checkTriangle(_triangle1, mousePos) || checkTriangle(_triangle2, mousePos))
65 return (true);
66 return (false);
67 }
68
69 void setPosition(sf::Vector2f pos) {
70 _pos = sf::Vector2f(pos.x - _tileSize.x / 2, pos.y - _tileSize.y / 2);
71 _triangle1.setPosition(_pos);
72 _triangle2.setPosition(_pos);
73 }
74
75 void setColor(sf::Color color) {
76 _triangle1.setFillColor(color);
77 _triangle2.setFillColor(color);
78 }
79
80 private:
81 sf::Vector2f _pos = sf::Vector2f(0, 0);
82 sf::Vector2f _tileSize;
83 sf::ConvexShape _triangle1;
84 sf::ConvexShape _triangle2;
85
86 };
87
88
89 #endif /* !DIAMOND_HPP_ */
90