Skip to content

Commit

Permalink
Added classes in order to draw the circumscribed circle of a triangle
Browse files Browse the repository at this point in the history
  • Loading branch information
ediardi committed Apr 3, 2024
1 parent 2b03ef3 commit 4ae48e5
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ add_executable(${PROJECT_NAME}
main.cpp
generated/src/Helper.cpp
env_fixes.h
Triangle.h
Point.h
Point.cpp
Triangle.cpp
Drawables.cpp
Drawables.h
)

###############################################################################
Expand Down
52 changes: 52 additions & 0 deletions Drawables.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Created by eciuc on 4/3/2024.
//

#include "Drawables.h"


void Drawables::add(sf::Vertex x) {
points.append(x);
}

void Drawables::add_triangle_vertex(sf::Vertex x) {
points.append(x);
trianglepoints++;
if(trianglepoints%3==0)
{
int n=points.getVertexCount();
triangles.emplace_back(points[n-1],points[n-2],points[n-3]);
}
}

void Drawables::add_circle(Point origin, float rad) {
sf::CircleShape cir(rad,60);

This comment has been minimized.

Copy link
@mcmarius

mcmarius Apr 13, 2024

Contributor

pune te rog nume de variabile/atribute mai descriptive acolo unde nu sunt notații consacrate, de exemplu aici circle, radius

cir.setOrigin(rad,rad);
cir.setPosition(origin.getx(),origin.gety());
cir.setOutlineColor(sf::Color::White);
cir.setFillColor(sf::Color::Transparent);
cir.setOutlineThickness(1);
circles.push_back(cir);
}

void Drawables::draw(sf::RenderTarget &target, sf::RenderStates states) const {
target.draw(points);
for(auto& cir:circles)
{
target.draw(cir);
}
sf::VertexArray linepoints(sf::Lines,0);
for(auto tr:triangles)
{
linepoints.append(tr.geta().tovertex());
linepoints.append(tr.getb().tovertex());

linepoints.append(tr.getb().tovertex());
linepoints.append(tr.getc().tovertex());

linepoints.append(tr.getc().tovertex());
linepoints.append(tr.geta().tovertex());
}
target.draw(linepoints);
}

25 changes: 25 additions & 0 deletions Drawables.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Created by eciuc on 4/3/2024.
//

#ifndef OOP_DRAWABLES_H
#define OOP_DRAWABLES_H

#include <SFML/Graphics.hpp>
#include "Triangle.h"

class Drawables : public sf::Drawable{

This comment has been minimized.

Copy link
@mcmarius

mcmarius Apr 13, 2024

Contributor

Uitându-mă pe modul de utilizare al clasei în main, mi se pare ciudat numele acestei clase raportat la ce vrea să semnifice sf::Drawable. Și nu înțeleg de ce ai moștenit din sf::Drawable dacă nu te folosești de interfață (pt că ai făcut privată funcția draw).

Poate ar fi mai ok să nu moștenești din sf::Drawable, iar clasa să se numească pur și simplu Application. Poți veni și cu alte idei, dar ce e acum e ciudat.


static sf::VertexArray points;
static std::vector<Triangle> triangles;
static std::vector<sf::CircleShape> circles;
static inline int trianglepoints=0;
public:
static void add(sf::Vertex x);
static void add_triangle_vertex(sf::Vertex x);
static void add_circle(Point origin,float rad);
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
};

#endif //OOP_DRAWABLES_H
31 changes: 31 additions & 0 deletions Point.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Created by eciuc on 4/3/2024.
//

#include <SFML/Graphics.hpp>
#include "Point.h"


Point::Point(sf::Vertex v){
this->x=v.position.x;
this->y=v.position.y;
}
Point::Point()= default;
Point::Point(int x,int y)
{
this->x= static_cast<float>(x);
this->y= static_cast<float>(y);
}
void Point::inc_color(){
color_index++;

This comment has been minimized.

Copy link
@mcmarius

mcmarius Apr 13, 2024

Contributor

aici nu verifici dacă ieși din vector

}
void Point::setx(float new_x){this->x=new_x;}
void Point::sety(float new_y){this->y=new_y;}
float Point::getx() const{return x;}
float Point::gety() const{return y;}
sf::Vertex Point::tovertex()
{
sf::Vector2f temp1(x,y);
sf::Vertex temp(temp1,color[color_index]);
return temp;
}
30 changes: 30 additions & 0 deletions Point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Created by eciuc on 4/3/2024.
//

#ifndef OOP_POINT_H
#define OOP_POINT_H

#include <SFML/Graphics.hpp>

class Point {
float x;
float y;
sf::Color color[7]={sf::Color::White,sf::Color::Yellow,sf::Color::Red

This comment has been minimized.

Copy link
@mcmarius

mcmarius Apr 13, 2024

Contributor

Preferă std::array în loc de vectori de tip C ca să nu trebuiască să repeți acel 7 sau să definești o constantă în plus; std::array are funcția .size().

Apoi indentează te rog codul într-un mod mai ușor de urmărit; ori pui editorul să facă asta automat, ori ceva de genul (dar te chinui degeaba dacă după pui editorul să refacă)

    // colors pt că sunt mai multe
    // ar trebui să meargă și cu deducție de tipuri: std::array colors { ... };
    std::array<sf::Color, 7> colors {
        sf::Color::White, sf::Color::Yellow,  sf::Color::Red,
        sf::Color::Green, sf::Color::Magenta, sf::Color::Cyan,
        sf::Color::Blue
    };
,sf::Color::Green, sf::Color::Magenta, sf::Color::Cyan
, sf::Color::Blue};
int color_index=0;
public:
Point(sf::Vertex v);
Point();
Point(int x,int y);
void inc_color();
void setx(float new_x);
void sety(float new_y);
float getx() const;
float gety() const;
sf::Vertex tovertex();
friend class Triangle;

This comment has been minimized.

Copy link
@mcmarius

mcmarius Apr 13, 2024

Contributor

Fără friend class

};

#endif //OOP_POINT_H
35 changes: 35 additions & 0 deletions Triangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Created by eciuc on 4/3/2024.
//

#include "Triangle.h"
#include "Drawables.h"
#include <SFML/Graphics.hpp>
#include <cmath>


Triangle::Triangle(sf::Vertex a,sf::Vertex b,sf::Vertex c): a(a),b(b),c(c){
cmc=Point();
calc_center();
}
void Triangle::calc_center(){ // transform in constructor, fac destructor , copiere operator <<
cmc.x= ((a.x * a.x + a.y * a.y) * (b.y - c.y) + (b.x * b.x + b.y * b.y) * (c.y - a.y) + (c.x * c.x + c.y * c.y) * (a.y - b.y))

This comment has been minimized.

Copy link
@mcmarius

mcmarius Apr 13, 2024

Contributor

Ca să nu fie problema de acces direct, definește funcții ajutătoare în punct:

float distance() const {  // sau ce reprezintă
    return x*x + y*y;
}

// sau
float centru(const Point p1, const Point p2) const {
    return (x*x + y*y) * (p1.getY() - p2.getY());
}

Iar apoi aici ar veni ceva de genul

void Triangle::calc_center() {
    cmc = Point(
        a.centru(b, c) + b.centru(c, a) + c.centru(a, b) / ..., // ce urmează în restul formulei
        // apoi pt y
        a.centru(c, b) + b.centru(a, c) + c.centru(b, a) / ...
    );
}

Sper că se înțelege ideea.

Alternativ, creezi un struct

struct Point {
    float x;
    float y;
};

Iar pt actuala clasă Point faci o altă clasă pt partea de desenat punctul în sine, dar acolo fără clase friend.

Insist cu asta pt că trebuie să te obișnuiești să definești funcții de nivel mai înalt și să te dezobișnuiești cu accesul direct la datele membru.

/ ((a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) * 2);
cmc.y= ((a.x * a.x + a.y * a.y) * (c.x - b.x) + (b.x * b.x + b.y * b.y) * (a.x - c.x) + (c.x * c.x + c.y * c.y) * (b.x - a.x))
/ ((a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) * 2);
radius = sqrtf((a.x - cmc.x) * (a.x - cmc.x) + (a.y - cmc.y) * (a.y - cmc.y));
Drawables::add_circle(cmc,radius);
Drawables::add(cmc.tovertex());
}

Point Triangle::geta() {
return a;
}

Point Triangle::getb() {
return b;
}

Point Triangle::getc() {
return c;
}
23 changes: 23 additions & 0 deletions Triangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by eciuc on 4/3/2024.
//

#ifndef OOP_TRIANGLE_H
#define OOP_TRIANGLE_H

#include "Point.h"
#include <SFML/Graphics.hpp>

class Triangle{
Point a,b,c;
Point cmc;

This comment has been minimized.

Copy link
@mcmarius

mcmarius Apr 13, 2024

Contributor

Nu îmi e clar de la ce vine cmc, poate dai un nume mai explicit

float radius;
public:
Triangle(sf::Vertex a,sf::Vertex b,sf::Vertex c);
void calc_center();

This comment has been minimized.

Copy link
@mcmarius

mcmarius Apr 13, 2024

Contributor

Funcția asta ar trebui să fie privată

Point geta();
Point getb();
Point getc();
};

#endif //OOP_TRIANGLE_H
Loading

0 comments on commit 4ae48e5

Please sign in to comment.