# HG changeset patch # User hegyi # Date 1111059905 0 # Node ID 7f4f2855fa11e305f9a57fcc01900f417350d95a # Parent decf8076d63cf9e43be410f9f31f30d3216b6e2b Magic triangle is a bit more DONE, and is already not only a triangle. diff -r decf8076d63c -r 7f4f2855fa11 src/work/peter/Makefile --- a/src/work/peter/Makefile Thu Mar 17 10:46:57 2005 +0000 +++ b/src/work/peter/Makefile Thu Mar 17 11:45:05 2005 +0000 @@ -1,2 +1,2 @@ triangle: canvas-test.cc - g++ canvas-test.cc -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs` + g++ canvas-test.cc -W -Wall -ansi -pedantic -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs` diff -r decf8076d63c -r 7f4f2855fa11 src/work/peter/canvas-test.cc --- a/src/work/peter/canvas-test.cc Thu Mar 17 10:46:57 2005 +0000 +++ b/src/work/peter/canvas-test.cc Thu Mar 17 11:45:05 2005 +0000 @@ -16,26 +16,45 @@ virtual ~CanvasExample(); private: + + ///Event handler function that handles dragging nodes of triangle + bool event_handler(GdkEvent* e, bool b); + + ///Event handler function that handles dragging triangle + bool tri_mover(GdkEvent* e, bool b); + + ///Coordinates of Weight Point of tirangle + Gnome::Art::Point * wp; + ///Array of nodes of planefigure + Gnome::Canvas::Ellipse ** nodes; + ///Sides of planefigure + Gnome::Canvas::Polygon * sides; + ///Group of graphical elements of triangle + Gnome::Canvas::Group triangle; + + ///Indicates whether the button of mouse is pressed or not + bool isbutton; + + ///Number Of Elements - the number of nodes int noe; - bool isbutton; + ///At this location was the mousebutton pressed. + ///It helps to calculate the distance of dragging. double clicked_x, clicked_y; + ///Remembers which Gnome::Canvas::Item was pressed. ///this variable is needed, because ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution Gnome::Canvas::Item * active_item; - bool event_handler(GdkEvent* e, bool b); - bool tri_mover(GdkEvent* e, bool b); - Gnome::Art::Point * wp; - Gnome::Canvas::Ellipse ** nodes; - Gnome::Canvas::Polygon * sides; - Gnome::Canvas::Group triangle; + }; +///When we click on the weight point we can drag the whole triangle. This function resolves it. bool CanvasExample::tri_mover(GdkEvent* e, bool b) { + b=b; switch(e->type) { case GDK_BUTTON_PRESS: @@ -50,13 +69,9 @@ case GDK_MOTION_NOTIFY: if(isbutton) { - //double x1, y1, x2, y2; - //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2); - //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2); - //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y); double dx=e->motion.x-clicked_x; double dy=e->motion.y-clicked_y; - for(int i=0;i<=noe/2;i++) + for(int i=0;i<=noe;i++) { nodes[i]->move(dx,dy); } @@ -64,7 +79,7 @@ clicked_y=e->motion.y; Gnome::Canvas::Points coos; - for(int i=0;iget_bounds(x1,y1,x2,y2); @@ -77,10 +92,15 @@ } default: break; } + return true; } +///This function moves only one node of triangle, +///but recalculate the location of wight point, +///and also redraw the sides of the planefigure. bool CanvasExample::event_handler(GdkEvent* e, bool b) { + b=b; switch(e->type) { case GDK_BUTTON_PRESS: @@ -109,7 +129,7 @@ double x_wp=0; double y_wp=0; - for(int i=0;iget_bounds(x1,y1,x2,y2); @@ -117,61 +137,58 @@ double y=(y1+y2)/2; coos.push_back(Gnome::Art::Point(x, y)); - if(i<3) - { - x_wp+=x; - y_wp+=y; - } + x_wp+=x; + y_wp+=y; } sides->property_points().set_value(coos); - x_wp/=3; - y_wp/=3; + x_wp/=noe; + y_wp/=noe; double x1,y1,x2,y2; - nodes[noe/2]->get_bounds(x1,y1,x2,y2); + nodes[noe]->get_bounds(x1,y1,x2,y2); double x=(x1+x2)/2; double y=(y1+y2)/2; dx=x_wp-x; dy=y_wp-y; - nodes[noe/2]->move(dx, dy); + nodes[noe]->move(dx, dy); clicked_x=e->motion.x; clicked_y=e->motion.y; } default: break; } + return true; } -CanvasExample::CanvasExample(double * coosarray, int numofels):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL) +CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL) { - noe=numofels; + noe=numofcoos/2; - int j=0; - double ax=coosarray[j++]; - double ay=coosarray[j++]; - double bx=coosarray[j++]; - double by=coosarray[j++]; - double cx=coosarray[j++]; - double cy=coosarray[j++]; + double x_wp=0; + double y_wp=0; Gnome::Canvas::Points coos; - for(int i=0;iproperty_width_pixels().set_value(10); - nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1]; + nodes=new Gnome::Canvas::Ellipse* [noe+1]; - for(int i=0; isignal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true)); } - wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3); + wp=new Gnome::Art::Point(x_wp/noe,y_wp/noe); - nodes[noe/2]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20); - *(nodes[noe/2]) << Gnome::Canvas::Properties::fill_color("blue"); - *(nodes[noe/2]) << Gnome::Canvas::Properties::outline_color("black"); - (nodes[noe/2])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true)); + nodes[noe]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20); + *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue"); + *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black"); + (nodes[noe])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true)); } @@ -205,7 +222,7 @@ CanvasExample m_canvas; }; -MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe) +MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc) { set_title (title); add(m_canvas); @@ -234,7 +251,5 @@ app.run(mainwin); } - printf("Usage:\n./triangle x1 y1 x2 y2 x3 y3\nWhere xi and yi are the coordinates of the points of the triangle\n"); - return 0; }