Skip to content

Commit 9705e02

Browse files
committed
improving scaling
1 parent 87bc1d3 commit 9705e02

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

CGTeaFrame.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ CGTeaFrame::CGTeaFrame(const wxString& title, const wxPoint& pos, const wxSize&
116116
auto *menuLayout = new wxMenu;
117117
menuLayout->Append(i, "Force-directed drawing", "Force-directed drawing");
118118
Connect(i, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CGTeaFrame::Layout));
119+
i++;
120+
menuLayout->Append(i, "&Fit Width", "Fit Graph to Width");
121+
Connect(i, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CGTeaFrame::OnFitWidth));
119122
auto menuHelp = new wxMenu;
120123
menuHelp->Append(wxID_ABOUT);
121124
wxMenuBar *menuBar = new wxMenuBar;
@@ -171,6 +174,7 @@ void CGTeaFrame::Generate(wxCommandEvent& event) {
171174

172175
void CGTeaFrame::Layout(wxCommandEvent& event) {
173176
int id = event.GetId();
177+
cout << id;
174178
// currentGraph = availableGenerators[id]->generate_with_force_directed(10,0,500,500);
175179
std::vector<cgtea_geometry::Point> pos = compute_force_directed(10, 10, 300, 300, currentGraph);
176180
int i = 0;
@@ -181,6 +185,44 @@ void CGTeaFrame::Layout(wxCommandEvent& event) {
181185
Refresh();
182186
}
183187

188+
void CGTeaFrame::OnFitWidth(wxCommandEvent& event)
189+
{
190+
// Get the current graph bounds
191+
double minX = std::numeric_limits<double>::max();
192+
double maxX = std::numeric_limits<double>::lowest();
193+
double minY = std::numeric_limits<double>::max();
194+
double maxY = std::numeric_limits<double>::lowest();
195+
196+
for_each_v_const(currentGraph, [&](Ver v) {
197+
const cgtea_geometry::Point pos = boost::get(boost::vertex_distance, currentGraph, v);
198+
minX = std::min(minX, pos.x);
199+
maxX = std::max(maxX, pos.x);
200+
minY = std::min(minY, pos.y);
201+
maxY = std::max(maxY, pos.y);
202+
});
203+
204+
// Get the panel width
205+
//auto panelSize = this->GetClientSize();
206+
auto panelSize = this->GetSizer()->GetItem(1)->GetSize();
207+
//auto panelSize = this->GetSizer()->GetItemById(0)->GetSize();
208+
cout << this->GetSizer()->GetItemCount();
209+
cout << panelSize.GetWidth() << endl;
210+
cout << panelSize.GetHeight() << endl;
211+
double padding = 20; // Leave some space for vertices on the edges
212+
double scaleX = (panelSize.GetWidth() - 2 * padding) / (maxX - minX);
213+
double scaleY = (panelSize.GetHeight() - 2 * padding) / (maxY - minY);
214+
215+
// Scale and center all vertices
216+
for_each_v(currentGraph, [&](const Ver v) {
217+
cgtea_geometry::Point pos = boost::get(boost::vertex_distance, currentGraph, v);
218+
pos.x = (pos.x - minX) * scaleX + padding;
219+
pos.y = (pos.y - minY) * scaleY + padding;
220+
boost::put(boost::vertex_distance, currentGraph, v, pos);
221+
});
222+
Refresh();
223+
}
224+
225+
184226
void CGTeaFrame::Report(wxCommandEvent& event) {
185227
int id = event.GetId();
186228
std::string report_results = availableReports[id - availableGenerators.size() - 1]->report(currentGraph);

CGTeaFrame.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ class CGTeaFrame: public wxFrame {
2929
std::vector<std::unique_ptr<GeneratorInterface>> availableGenerators;
3030
std::vector<std::unique_ptr<ReportInterface>> availableReports;
3131
std::vector<std::unique_ptr<ActionInterface>> availableActions;
32+
protected:
33+
static const long ID_FIT_WIDTH = wxID_HIGHEST + 1;
34+
void OnFitWidth(wxCommandEvent &event);
3235

33-
// std::tuple<Cycle, Complete, Antiprism> availableGenerators = std::make_tuple(Cycle(), Complete(), Antiprism());
3436
private:
35-
3637
void Generate(wxCommandEvent &event);
3738
void Report(wxCommandEvent &event);
3839
void Layout(wxCommandEvent &event);

0 commit comments

Comments
 (0)