COIN-OR::LEMON - Graph Library

Changeset 1221:6706c788ebb5 in lemon-0.x


Ignore:
Timestamp:
03/16/05 18:31:04 (19 years ago)
Author:
Hegyi Péter
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1641
Message:

Magic triangle is READY.

Location:
src/work/peter
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/work/peter/Makefile

    r922 r1221  
    1 hier: hierarchygraph.h hierarchygraph_test.cc
    2         g++ -Wall -W -I../.. -I../klao -I../../lemon hierarchygraph_test.cc -o test
    3 
    4 edge: edgepathgraph_test.cc edgepathgraph.h
    5         g++ -Wall -W -I../.. -I../klao -I../../lemon edgepathgraph_test.cc -o test
    6 
     1triangle: canvas-test.cc
     2        g++ canvas-test.cc -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs`
  • src/work/peter/canvas-test.cc

    r1212 r1221  
    1313
    1414public:
    15         CanvasExample();
     15        CanvasExample(double *, int);
    1616        virtual ~CanvasExample();
    1717
    1818private:
     19        int noe;
     20
     21        bool isbutton;
     22        double clicked_x, clicked_y;
     23
     24        ///this variable is needed, because
     25        ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
     26        ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
     27        Gnome::Canvas::Item * active_item;
     28
    1929        bool event_handler(GdkEvent* e, bool b);
    20         Gnome::Canvas::Points coos;
     30        bool tri_mover(GdkEvent* e, bool b);
    2131        Gnome::Art::Point * wp;
    2232        Gnome::Canvas::Ellipse ** nodes;
     
    2535};
    2636
     37bool CanvasExample::tri_mover(GdkEvent* e, bool b)
     38{
     39        switch(e->type)
     40        {
     41                case GDK_BUTTON_PRESS:
     42                        clicked_x=e->button.x;
     43                        clicked_y=e->button.y;
     44                        isbutton=true;
     45                        break;
     46                case GDK_BUTTON_RELEASE:
     47                        isbutton=false;
     48                        active_item=NULL;
     49                        break;
     50                case GDK_MOTION_NOTIFY:
     51                        if(isbutton)
     52                        {
     53                                //double x1, y1, x2, y2;
     54                                //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
     55                                //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
     56                                //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
     57                                double dx=e->motion.x-clicked_x;
     58                                double dy=e->motion.y-clicked_y;
     59                                for(int i=0;i<=noe/2;i++)
     60                                {
     61                                        nodes[i]->move(dx,dy);
     62                                }
     63                                clicked_x=e->motion.x;
     64                                clicked_y=e->motion.y;
     65
     66                                Gnome::Canvas::Points coos;
     67                                for(int i=0;i<noe/2;i++)
     68                                {
     69                                        double x1,y1,x2,y2;
     70                                        nodes[i]->get_bounds(x1,y1,x2,y2);
     71                                        double x=(x1+x2)/2;
     72                                        double y=(y1+y2)/2;
     73                                        coos.push_back(Gnome::Art::Point(x, y));
     74                                }
     75                                sides->property_points().set_value(coos);
     76
     77                        }
     78                default: break;
     79        }
     80}
     81
    2782bool CanvasExample::event_handler(GdkEvent* e, bool b)
    2883{
    29         bool isbutton=true;
    3084        switch(e->type)
    3185        {
    32                 case GDK_BUTTON_PRESS: printf("Node is pressed!\n"); break;
    33                 //case GDK_BUTTON_RELEASE: printf("Node is released!\n"); break;
    34                 default: isbutton=false; break;
    35         }
    36         if(isbutton)
    37         {
    38                 //(get_item_at(e->button.x, e->button.y))->move(5,5);
    39                 (get_item_at(e->button.x, e->button.y))->hide();
    40         }
    41 }
    42 
    43 CanvasExample::CanvasExample():triangle(*(root()), 0, 0)
    44 {
    45         double ax=100;
    46         double ay=100;
    47         double bx=-100;
    48         double by=100;
    49         double cx=0;
    50         double cy=-100;
    51         coos.push_back(Gnome::Art::Point(100, 100));
    52         coos.push_back(Gnome::Art::Point(-100, 100));
    53         coos.push_back(Gnome::Art::Point(0, -100));
     86                case GDK_BUTTON_PRESS:
     87                        clicked_x=e->button.x;
     88                        clicked_y=e->button.y;
     89                        active_item=(get_item_at(e->button.x, e->button.y));
     90                        isbutton=true;
     91                        break;
     92                case GDK_BUTTON_RELEASE:
     93                        isbutton=false;
     94                        active_item=NULL;
     95                        break;
     96                case GDK_MOTION_NOTIFY:
     97                        if(isbutton)
     98                        {
     99                                //double x1, y1, x2, y2;
     100                                //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
     101                                //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
     102                                //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
     103                                double dx=e->motion.x-clicked_x;
     104                                double dy=e->motion.y-clicked_y;
     105                                active_item->move(dx, dy);
     106
     107                                Gnome::Canvas::Points coos;
     108
     109                                double x_wp=0;
     110                                double y_wp=0;
     111
     112                                for(int i=0;i<noe/2;i++)
     113                                {
     114                                        double x1,y1,x2,y2;
     115                                        nodes[i]->get_bounds(x1,y1,x2,y2);
     116                                        double x=(x1+x2)/2;
     117                                        double y=(y1+y2)/2;
     118                                        coos.push_back(Gnome::Art::Point(x, y));
     119
     120                                        if(i<3)
     121                                        {
     122                                                x_wp+=x;
     123                                                y_wp+=y;
     124                                        }
     125                                }
     126
     127                                sides->property_points().set_value(coos);
     128
     129                                x_wp/=3;
     130                                y_wp/=3;
     131
     132                                double x1,y1,x2,y2;
     133                                nodes[noe/2]->get_bounds(x1,y1,x2,y2);
     134                                double x=(x1+x2)/2;
     135                                double y=(y1+y2)/2;
     136
     137                                dx=x_wp-x;
     138                                dy=y_wp-y;
     139                                nodes[noe/2]->move(dx, dy);
     140
     141                                clicked_x=e->motion.x;
     142                                clicked_y=e->motion.y;
     143                        }
     144                default: break;
     145        }
     146}
     147
     148CanvasExample::CanvasExample(double * coosarray, int numofels):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
     149{
     150        noe=numofels;
     151
     152        int j=0;
     153        double ax=coosarray[j++];
     154        double ay=coosarray[j++];
     155        double bx=coosarray[j++];
     156        double by=coosarray[j++];
     157        double cx=coosarray[j++];
     158        double cy=coosarray[j++];
     159
     160        Gnome::Canvas::Points coos;
     161        for(int i=0;i<noe;i++)
     162        {
     163                double x=coosarray[i++];
     164                double y=coosarray[i];
     165                coos.push_back(Gnome::Art::Point(x, y));
     166        }
    54167
    55168        sides=new Gnome::Canvas::Polygon(triangle, coos);
     
    57170        sides->property_width_pixels().set_value(10);
    58171
    59         nodes=new ( Gnome::Canvas::Ellipse * ) [4];
    60 
    61         for(int i=0; i<3;i++)
     172        nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1];
     173
     174        for(int i=0; i<noe/2;i++)
    62175        {
    63176                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);
     
    69182        wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3);
    70183
    71         nodes[3]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
    72         *(nodes[3]) << Gnome::Canvas::Properties::fill_color("blue");
    73         *(nodes[3]) << Gnome::Canvas::Properties::outline_color("black");
     184        nodes[noe/2]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
     185        *(nodes[noe/2]) << Gnome::Canvas::Properties::fill_color("blue");
     186        *(nodes[noe/2]) << Gnome::Canvas::Properties::outline_color("black");
     187        (nodes[noe/2])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
    74188
    75189
     
    85199{
    86200public:
    87         MainWin(const std::string& title);
     201        MainWin(const std::string& title, double *, int);
    88202
    89203protected:
     
    92206};
    93207
    94 MainWin::MainWin(const std::string& title)
     208MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe)
    95209{
    96210        set_title (title);
     
    105219int main(int argc, char *argv[])
    106220{
    107         Gnome::Canvas::init();
    108         Gtk::Main app(argc, argv);
    109 
    110         MainWin mainwin("Gnome::Canvas Example");
    111         app.run(mainwin);
     221        if((argc>=7)&&( (argc/2)!=( (argc+1)/2 ) ) )
     222        {
     223                double * coosarray=new double[argc];
     224
     225                for(int i=1;i<argc;i++)
     226                {
     227                        coosarray[i-1]=atof(argv[i]);
     228                }
     229
     230                Gnome::Canvas::init();
     231                Gtk::Main app(argc, argv);
     232
     233                MainWin mainwin("Magic Triangle",coosarray,argc-1);
     234                app.run(mainwin);
     235        }
     236
     237        printf("Usage:\n./triangle x1 y1 x2 y2 x3 y3\nWhere xi and yi are the coordinates of the points of the triangle\n");
    112238
    113239        return 0;
Note: See TracChangeset for help on using the changeset viewer.