Magic triangle is a bit more DONE, and is already not only a triangle.
1.1 --- a/src/work/peter/Makefile Thu Mar 17 10:46:57 2005 +0000
1.2 +++ b/src/work/peter/Makefile Thu Mar 17 11:45:05 2005 +0000
1.3 @@ -1,2 +1,2 @@
1.4 triangle: canvas-test.cc
1.5 - g++ canvas-test.cc -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs`
1.6 + g++ canvas-test.cc -W -Wall -ansi -pedantic -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs`
2.1 --- a/src/work/peter/canvas-test.cc Thu Mar 17 10:46:57 2005 +0000
2.2 +++ b/src/work/peter/canvas-test.cc Thu Mar 17 11:45:05 2005 +0000
2.3 @@ -16,26 +16,45 @@
2.4 virtual ~CanvasExample();
2.5
2.6 private:
2.7 +
2.8 + ///Event handler function that handles dragging nodes of triangle
2.9 + bool event_handler(GdkEvent* e, bool b);
2.10 +
2.11 + ///Event handler function that handles dragging triangle
2.12 + bool tri_mover(GdkEvent* e, bool b);
2.13 +
2.14 + ///Coordinates of Weight Point of tirangle
2.15 + Gnome::Art::Point * wp;
2.16 + ///Array of nodes of planefigure
2.17 + Gnome::Canvas::Ellipse ** nodes;
2.18 + ///Sides of planefigure
2.19 + Gnome::Canvas::Polygon * sides;
2.20 + ///Group of graphical elements of triangle
2.21 + Gnome::Canvas::Group triangle;
2.22 +
2.23 + ///Indicates whether the button of mouse is pressed or not
2.24 + bool isbutton;
2.25 +
2.26 + ///Number Of Elements - the number of nodes
2.27 int noe;
2.28
2.29 - bool isbutton;
2.30 + ///At this location was the mousebutton pressed.
2.31 + ///It helps to calculate the distance of dragging.
2.32 double clicked_x, clicked_y;
2.33
2.34 + ///Remembers which Gnome::Canvas::Item was pressed.
2.35 ///this variable is needed, because
2.36 ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
2.37 ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
2.38 Gnome::Canvas::Item * active_item;
2.39
2.40 - bool event_handler(GdkEvent* e, bool b);
2.41 - bool tri_mover(GdkEvent* e, bool b);
2.42 - Gnome::Art::Point * wp;
2.43 - Gnome::Canvas::Ellipse ** nodes;
2.44 - Gnome::Canvas::Polygon * sides;
2.45 - Gnome::Canvas::Group triangle;
2.46 +
2.47 };
2.48
2.49 +///When we click on the weight point we can drag the whole triangle. This function resolves it.
2.50 bool CanvasExample::tri_mover(GdkEvent* e, bool b)
2.51 {
2.52 + b=b;
2.53 switch(e->type)
2.54 {
2.55 case GDK_BUTTON_PRESS:
2.56 @@ -50,13 +69,9 @@
2.57 case GDK_MOTION_NOTIFY:
2.58 if(isbutton)
2.59 {
2.60 - //double x1, y1, x2, y2;
2.61 - //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
2.62 - //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
2.63 - //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
2.64 double dx=e->motion.x-clicked_x;
2.65 double dy=e->motion.y-clicked_y;
2.66 - for(int i=0;i<=noe/2;i++)
2.67 + for(int i=0;i<=noe;i++)
2.68 {
2.69 nodes[i]->move(dx,dy);
2.70 }
2.71 @@ -64,7 +79,7 @@
2.72 clicked_y=e->motion.y;
2.73
2.74 Gnome::Canvas::Points coos;
2.75 - for(int i=0;i<noe/2;i++)
2.76 + for(int i=0;i<noe;i++)
2.77 {
2.78 double x1,y1,x2,y2;
2.79 nodes[i]->get_bounds(x1,y1,x2,y2);
2.80 @@ -77,10 +92,15 @@
2.81 }
2.82 default: break;
2.83 }
2.84 + return true;
2.85 }
2.86
2.87 +///This function moves only one node of triangle,
2.88 +///but recalculate the location of wight point,
2.89 +///and also redraw the sides of the planefigure.
2.90 bool CanvasExample::event_handler(GdkEvent* e, bool b)
2.91 {
2.92 + b=b;
2.93 switch(e->type)
2.94 {
2.95 case GDK_BUTTON_PRESS:
2.96 @@ -109,7 +129,7 @@
2.97 double x_wp=0;
2.98 double y_wp=0;
2.99
2.100 - for(int i=0;i<noe/2;i++)
2.101 + for(int i=0;i<noe;i++)
2.102 {
2.103 double x1,y1,x2,y2;
2.104 nodes[i]->get_bounds(x1,y1,x2,y2);
2.105 @@ -117,61 +137,58 @@
2.106 double y=(y1+y2)/2;
2.107 coos.push_back(Gnome::Art::Point(x, y));
2.108
2.109 - if(i<3)
2.110 - {
2.111 - x_wp+=x;
2.112 - y_wp+=y;
2.113 - }
2.114 + x_wp+=x;
2.115 + y_wp+=y;
2.116 }
2.117
2.118 sides->property_points().set_value(coos);
2.119
2.120 - x_wp/=3;
2.121 - y_wp/=3;
2.122 + x_wp/=noe;
2.123 + y_wp/=noe;
2.124
2.125 double x1,y1,x2,y2;
2.126 - nodes[noe/2]->get_bounds(x1,y1,x2,y2);
2.127 + nodes[noe]->get_bounds(x1,y1,x2,y2);
2.128 double x=(x1+x2)/2;
2.129 double y=(y1+y2)/2;
2.130
2.131 dx=x_wp-x;
2.132 dy=y_wp-y;
2.133 - nodes[noe/2]->move(dx, dy);
2.134 + nodes[noe]->move(dx, dy);
2.135
2.136 clicked_x=e->motion.x;
2.137 clicked_y=e->motion.y;
2.138 }
2.139 default: break;
2.140 }
2.141 + return true;
2.142 }
2.143
2.144 -CanvasExample::CanvasExample(double * coosarray, int numofels):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
2.145 +CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
2.146 {
2.147 - noe=numofels;
2.148 + noe=numofcoos/2;
2.149
2.150 - int j=0;
2.151 - double ax=coosarray[j++];
2.152 - double ay=coosarray[j++];
2.153 - double bx=coosarray[j++];
2.154 - double by=coosarray[j++];
2.155 - double cx=coosarray[j++];
2.156 - double cy=coosarray[j++];
2.157 + double x_wp=0;
2.158 + double y_wp=0;
2.159
2.160 Gnome::Canvas::Points coos;
2.161 - for(int i=0;i<noe;i++)
2.162 + for(int i=0;i<numofcoos;i++)
2.163 {
2.164 double x=coosarray[i++];
2.165 double y=coosarray[i];
2.166 coos.push_back(Gnome::Art::Point(x, y));
2.167 +
2.168 + x_wp+=x;
2.169 + y_wp+=y;
2.170 +
2.171 }
2.172
2.173 sides=new Gnome::Canvas::Polygon(triangle, coos);
2.174 *sides << Gnome::Canvas::Properties::outline_color("green");
2.175 sides->property_width_pixels().set_value(10);
2.176
2.177 - nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1];
2.178 + nodes=new Gnome::Canvas::Ellipse* [noe+1];
2.179
2.180 - for(int i=0; i<noe/2;i++)
2.181 + for(int i=0; i<noe;i++)
2.182 {
2.183 nodes[i]= new Gnome::Canvas::Ellipse(triangle, coos[i].get_x()-20, coos[i].get_y()-20, coos[i].get_x()+20, coos[i].get_y()+20);
2.184 *(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
2.185 @@ -179,12 +196,12 @@
2.186 (nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true));
2.187 }
2.188
2.189 - wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3);
2.190 + wp=new Gnome::Art::Point(x_wp/noe,y_wp/noe);
2.191
2.192 - nodes[noe/2]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
2.193 - *(nodes[noe/2]) << Gnome::Canvas::Properties::fill_color("blue");
2.194 - *(nodes[noe/2]) << Gnome::Canvas::Properties::outline_color("black");
2.195 - (nodes[noe/2])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
2.196 + nodes[noe]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
2.197 + *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
2.198 + *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
2.199 + (nodes[noe])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
2.200
2.201
2.202 }
2.203 @@ -205,7 +222,7 @@
2.204 CanvasExample m_canvas;
2.205 };
2.206
2.207 -MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe)
2.208 +MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc)
2.209 {
2.210 set_title (title);
2.211 add(m_canvas);
2.212 @@ -234,7 +251,5 @@
2.213 app.run(mainwin);
2.214 }
2.215
2.216 - printf("Usage:\n./triangle x1 y1 x2 y2 x3 y3\nWhere xi and yi are the coordinates of the points of the triangle\n");
2.217 -
2.218 return 0;
2.219 }