Skip to content

Commit 7628e2d

Browse files
committed
some refactoring, adding vertex shapes
1 parent 26e6567 commit 7628e2d

File tree

5 files changed

+120
-25
lines changed

5 files changed

+120
-25
lines changed

BasicDrawPane.cpp

Lines changed: 96 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ END_EVENT_TABLE()
3131
void BasicDrawPane::mouseMoved(wxMouseEvent& event) {}
3232
void BasicDrawPane::mouseDown(wxMouseEvent& event) {
3333
// boost::put(boost::vertex_distance, g, v, pos[i]);
34-
Graph& g = ((CGTeaFrame*)this->m_parent)->currentGraph;
35-
Ver vv = boost::num_vertices(g);
34+
Graph& g = static_cast<CGTeaFrame*>(this->m_parent)->currentGraph;
35+
const Ver vv = boost::num_vertices(g);
3636
boost::add_vertex(vv, g);
37-
cgtea_geometry::Point p(event.GetPosition().x,event.GetPosition().y);
37+
const cgtea_geometry::Point p(event.GetPosition().x,event.GetPosition().y);
3838
boost::put(boost::vertex_distance, g, vv, p);
3939
Refresh();
4040
// int radius = 20;
@@ -158,10 +158,10 @@ void BasicDrawPane::render(wxPaintDC& dc) {
158158

159159
void BasicDrawPane::drawEdges(const Graph &g, wxGraphicsContext* gc) {
160160
for_each_e_const(g, [&](Edge e) {
161-
Ver src = boost::source(e,g);
162-
Ver tgt = boost::target(e,g);
163-
cgtea_geometry::Point src_pos = boost::get(boost::vertex_distance, g, src);
164-
cgtea_geometry::Point tgt_pos = boost::get(boost::vertex_distance, g, tgt);
161+
const Ver src = boost::source(e,g);
162+
const Ver tgt = boost::target(e,g);
163+
const cgtea_geometry::Point src_pos = boost::get(boost::vertex_distance, g, src);
164+
const cgtea_geometry::Point tgt_pos = boost::get(boost::vertex_distance, g, tgt);
165165
gc->SetPen(wxPen(wxColor(0, 0, 0), 2)); // black line, 3 pixels thick
166166
wxGraphicsPath path = gc->CreatePath();
167167
path.MoveToPoint(src_pos.x, src_pos.y);
@@ -170,25 +170,99 @@ void BasicDrawPane::drawEdges(const Graph &g, wxGraphicsContext* gc) {
170170
});
171171
}
172172

173+
// void BasicDrawPane::drawVertices(const Graph &g, wxGraphicsContext* gc) {
174+
// for_each_v_const(g, [&](Ver v) {
175+
// int color = boost::get(vertex_color, g,v);
176+
// gc->SetPen(wxPen(wxColor(255, 0, 0), 1)); // 5-pixels-thick red outline
177+
// cgtea_geometry::Point pos = boost::get(boost::vertex_distance, g, v);
178+
// gc->SetBrush(wxBrush( wxColour(255, 255, 255, 255))); // green filling
179+
// wxGraphicsPath pathBackground = gc->CreatePath();
180+
// pathBackground.AddCircle(pos.x, pos.y, 20 );
181+
// gc->FillPath(pathBackground);
182+
//
183+
// gc->SetBrush(wxBrush( distinctColors[color + 1]));
184+
// wxGraphicsPath path = gc->CreatePath();
185+
// path.AddCircle(pos.x, pos.y, 20 );
186+
// gc->FillPath(path);
187+
//
188+
// gc->SetBrush(wxBrush( wxColour(0, 0, 0, 255)));
189+
// int tmp = boost::get(boost::vertex_index, g, v) + 1;
190+
// wxString mystring = wxString::Format(wxT("%i"),tmp);
191+
// wxDouble w, h;
192+
// gc->GetTextExtent(mystring, &w, &h, nullptr, nullptr);
193+
// gc->DrawText(mystring, pos.x-w/2, pos.y-h/2);
194+
// });
195+
// }
196+
197+
void BasicDrawPane::drawShape(wxGraphicsContext* gc, VertexShape shape,
198+
const cgtea_geometry::Point& pos, double size) {
199+
switch(shape) {
200+
case VertexShape::Square:
201+
drawSquare(gc, pos, size);
202+
break;
203+
case VertexShape::Triangle:
204+
drawTriangle(gc, pos, size);
205+
break;
206+
case VertexShape::Diamond:
207+
drawDiamond(gc, pos, size);
208+
break;
209+
case VertexShape::Circle:
210+
default:
211+
drawCircle(gc, pos, size);
212+
break;
213+
}
214+
}
215+
216+
void BasicDrawPane::drawCircle(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size) {
217+
wxGraphicsPath path = gc->CreatePath();
218+
path.AddCircle(pos.x, pos.y, size);
219+
gc->FillPath(path);
220+
}
221+
222+
void BasicDrawPane::drawSquare(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size) {
223+
wxGraphicsPath path = gc->CreatePath();
224+
path.AddRectangle(pos.x - size, pos.y - size, size * 2, size * 2);
225+
gc->FillPath(path);
226+
}
227+
228+
void BasicDrawPane::drawTriangle(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size) {
229+
wxGraphicsPath path = gc->CreatePath();
230+
path.MoveToPoint(pos.x, pos.y - size);
231+
path.AddLineToPoint(pos.x - size, pos.y + size);
232+
path.AddLineToPoint(pos.x + size, pos.y + size);
233+
path.CloseSubpath();
234+
gc->FillPath(path);
235+
}
236+
237+
void BasicDrawPane::drawDiamond(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size) {
238+
wxGraphicsPath path = gc->CreatePath();
239+
path.MoveToPoint(pos.x, pos.y - size);
240+
path.AddLineToPoint(pos.x + size, pos.y);
241+
path.AddLineToPoint(pos.x, pos.y + size);
242+
path.AddLineToPoint(pos.x - size, pos.y);
243+
path.CloseSubpath();
244+
gc->FillPath(path);
245+
}
246+
173247
void BasicDrawPane::drawVertices(const Graph &g, wxGraphicsContext* gc) {
174248
for_each_v_const(g, [&](Ver v) {
175-
int color = boost::get(vertex_color, g,v);
176-
gc->SetPen(wxPen(wxColor(255, 0, 0), 1)); // 5-pixels-thick red outline
177-
cgtea_geometry::Point pos = boost::get(boost::vertex_distance, g, v);
178-
// gc->DrawCircle(wxPoint(pos.x, pos.y), 20 /* radius */ );
179-
gc->SetBrush(wxBrush( wxColour(255, 255, 255, 255))); // green filling
180-
wxGraphicsPath pathBackground = gc->CreatePath();
181-
pathBackground.AddCircle(pos.x, pos.y, 20 );
182-
gc->FillPath(pathBackground);
183-
184-
gc->SetBrush(wxBrush( distinctColors[color + 1]));
185-
wxGraphicsPath path = gc->CreatePath();
186-
path.AddCircle(pos.x, pos.y, 20 );
187-
gc->FillPath(path);
249+
const int color = boost::get(vertex_color, g, v);
250+
const cgtea_geometry::Point pos = boost::get(boost::vertex_distance, g, v);
251+
auto shape = VertexShape::Diamond;
252+
253+
// Draw a white background
254+
gc->SetPen(wxPen(wxColor(255, 0, 0), 1));
255+
gc->SetBrush(wxBrush(wxColour(255, 255, 255, 255)));
256+
drawShape(gc, shape, pos, 20);
257+
258+
// Draw a colored shape
259+
gc->SetBrush(wxBrush(distinctColors[color + 1]));
260+
drawShape(gc, shape, pos, 20);
188261

189-
gc->SetBrush(wxBrush( wxColour(0, 0, 0, 255)));
262+
// Draw a vertex number
263+
gc->SetBrush(wxBrush(wxColour(0, 0, 0, 255)));
190264
int tmp = boost::get(boost::vertex_index, g, v) + 1;
191-
wxString mystring = wxString::Format(wxT("%i"),tmp);
265+
wxString mystring = wxString::Format(wxT("%i"), tmp);
192266
wxDouble w, h;
193267
gc->GetTextExtent(mystring, &w, &h, nullptr, nullptr);
194268
gc->DrawText(mystring, pos.x-w/2, pos.y-h/2);

BasicDrawPane.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ class BasicDrawPane : public wxPanel
3030

3131
void drawEdges(const Graph &g, wxGraphicsContext* gc);
3232
void drawVertices(const Graph &g, wxGraphicsContext* gc);
33-
// some useful events
34-
33+
void drawShape(wxGraphicsContext* gc, VertexShape shape, const cgtea_geometry::Point& pos, double size);
34+
void drawCircle(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size);
35+
void drawSquare(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size);
36+
void drawTriangle(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size);
37+
void drawDiamond(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size);
38+
3539
void mouseMoved(wxMouseEvent& event);
3640
void mouseDown(wxMouseEvent& event);
3741
void mouseWheelMoved(wxMouseEvent& event);

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ file(GLOB Actions "actions/*.cpp")
1212
file(GLOB ConjectureCheck "ConjectureCheck/*.cpp")
1313
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
1414
#set( CMAKE_CXX_FLAGS "`wx-config --cxxflags --libs`")
15-
add_executable(CGTea CGTeaApp.cpp BasicDrawPane.cpp CGTeaFrame.cpp CGTeaSidebar.cpp ${Reports} ${Generators} ${Actions})
15+
add_executable(CGTea CGTeaApp.cpp BasicDrawPane.cpp CGTeaFrame.cpp CGTeaSidebar.cpp ${Reports} ${Generators} ${Actions}
16+
const_values.h)
1617
add_executable(BoostTest ${Reports} ${Generators} ${BoostTestSrc} ${ConjectureCheck} ${Actions})
1718
target_link_libraries(CGTea ${wxWidgets_LIBRARIES} ${Boost_LIBRARIES})
1819
target_link_libraries(BoostTest ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})

const_values.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Created by rostam on 15.05.25.
3+
//
4+
5+
#ifndef CONST_VALUES_H
6+
#define CONST_VALUES_H
7+
8+
enum class VertexShape {
9+
Circle,
10+
Square,
11+
Triangle,
12+
Diamond
13+
};
14+
15+
#endif //CONST_VALUES_H

datatypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <random>
1212
#include "generators/point.h"
1313
#include <boost/graph/exterior_property.hpp>
14+
#include "const_values.h"
1415

1516
#define printVarNameAndItsValue(x) cout<<"The value of variable "<<(#x)<<" is => "<<x<<"\n"
1617

0 commit comments

Comments
 (0)