COIN-OR::LEMON - Graph Library

Changeset 1224:7f4f2855fa11 in lemon-0.x for src/work/peter


Ignore:
Timestamp:
03/17/05 12:45:05 (19 years ago)
Author:
Hegyi Péter
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1644
Message:

Magic triangle is a bit more DONE, and is already not only a triangle.

Location:
src/work/peter
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/work/peter/Makefile

    r1221 r1224  
    11triangle: canvas-test.cc
    2         g++ canvas-test.cc -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs`
     2        g++ canvas-test.cc -W -Wall -ansi -pedantic -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs`
  • src/work/peter/canvas-test.cc

    r1221 r1224  
    1717
    1818private:
     19
     20        ///Event handler function that handles dragging nodes of triangle
     21        bool event_handler(GdkEvent* e, bool b);
     22
     23        ///Event handler function that handles dragging triangle
     24        bool tri_mover(GdkEvent* e, bool b);
     25
     26        ///Coordinates of Weight Point of tirangle
     27        Gnome::Art::Point * wp;
     28        ///Array of nodes of planefigure
     29        Gnome::Canvas::Ellipse ** nodes;
     30        ///Sides of planefigure
     31        Gnome::Canvas::Polygon * sides;
     32        ///Group of graphical elements of triangle
     33        Gnome::Canvas::Group triangle;
     34
     35        ///Indicates whether the button of mouse is pressed or not
     36        bool isbutton;
     37
     38        ///Number Of Elements - the number of nodes
    1939        int noe;
    2040
    21         bool isbutton;
     41        ///At this location was the mousebutton pressed.
     42        ///It helps to calculate the distance of dragging.
    2243        double clicked_x, clicked_y;
    2344
     45        ///Remembers which Gnome::Canvas::Item was pressed.
    2446        ///this variable is needed, because
    2547        ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
     
    2749        Gnome::Canvas::Item * active_item;
    2850
    29         bool event_handler(GdkEvent* e, bool b);
    30         bool tri_mover(GdkEvent* e, bool b);
    31         Gnome::Art::Point * wp;
    32         Gnome::Canvas::Ellipse ** nodes;
    33         Gnome::Canvas::Polygon * sides;
    34         Gnome::Canvas::Group triangle;
     51
    3552};
    3653
     54///When we click on the weight point we can drag the whole triangle. This function resolves it.
    3755bool CanvasExample::tri_mover(GdkEvent* e, bool b)
    3856{
     57        b=b;
    3958        switch(e->type)
    4059        {
     
    5170                        if(isbutton)
    5271                        {
    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);
    5772                                double dx=e->motion.x-clicked_x;
    5873                                double dy=e->motion.y-clicked_y;
    59                                 for(int i=0;i<=noe/2;i++)
     74                                for(int i=0;i<=noe;i++)
    6075                                {
    6176                                        nodes[i]->move(dx,dy);
     
    6580
    6681                                Gnome::Canvas::Points coos;
    67                                 for(int i=0;i<noe/2;i++)
     82                                for(int i=0;i<noe;i++)
    6883                                {
    6984                                        double x1,y1,x2,y2;
     
    7893                default: break;
    7994        }
    80 }
    81 
     95        return true;
     96}
     97
     98///This function moves only one node of triangle,
     99///but recalculate the location of wight point,
     100///and also redraw the sides of the planefigure.
    82101bool CanvasExample::event_handler(GdkEvent* e, bool b)
    83102{
     103        b=b;
    84104        switch(e->type)
    85105        {
     
    110130                                double y_wp=0;
    111131
    112                                 for(int i=0;i<noe/2;i++)
     132                                for(int i=0;i<noe;i++)
    113133                                {
    114134                                        double x1,y1,x2,y2;
     
    118138                                        coos.push_back(Gnome::Art::Point(x, y));
    119139
    120                                         if(i<3)
    121                                         {
    122                                                 x_wp+=x;
    123                                                 y_wp+=y;
    124                                         }
     140                                        x_wp+=x;
     141                                        y_wp+=y;
    125142                                }
    126143
    127144                                sides->property_points().set_value(coos);
    128145
    129                                 x_wp/=3;
    130                                 y_wp/=3;
     146                                x_wp/=noe;
     147                                y_wp/=noe;
    131148
    132149                                double x1,y1,x2,y2;
    133                                 nodes[noe/2]->get_bounds(x1,y1,x2,y2);
     150                                nodes[noe]->get_bounds(x1,y1,x2,y2);
    134151                                double x=(x1+x2)/2;
    135152                                double y=(y1+y2)/2;
     
    137154                                dx=x_wp-x;
    138155                                dy=y_wp-y;
    139                                 nodes[noe/2]->move(dx, dy);
     156                                nodes[noe]->move(dx, dy);
    140157
    141158                                clicked_x=e->motion.x;
     
    144161                default: break;
    145162        }
    146 }
    147 
    148 CanvasExample::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++];
     163        return true;
     164}
     165
     166CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
     167{
     168        noe=numofcoos/2;
     169
     170        double x_wp=0;
     171        double y_wp=0;
    159172
    160173        Gnome::Canvas::Points coos;
    161         for(int i=0;i<noe;i++)
     174        for(int i=0;i<numofcoos;i++)
    162175        {
    163176                double x=coosarray[i++];
    164177                double y=coosarray[i];
    165178                coos.push_back(Gnome::Art::Point(x, y));
     179
     180                x_wp+=x;
     181                y_wp+=y;
     182
    166183        }
    167184
     
    170187        sides->property_width_pixels().set_value(10);
    171188
    172         nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1];
    173 
    174         for(int i=0; i<noe/2;i++)
     189        nodes=new Gnome::Canvas::Ellipse* [noe+1];
     190
     191        for(int i=0; i<noe;i++)
    175192        {
    176193                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);
     
    180197        }
    181198
    182         wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3);
    183 
    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));
     199        wp=new Gnome::Art::Point(x_wp/noe,y_wp/noe);
     200
     201        nodes[noe]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
     202        *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
     203        *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
     204        (nodes[noe])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
    188205
    189206
     
    206223};
    207224
    208 MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe)
     225MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc)
    209226{
    210227        set_title (title);
     
    235252        }
    236253
    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");
    238 
    239254        return 0;
    240255}
Note: See TracChangeset for help on using the changeset viewer.