Magic anyangle is Faster, harder, Blumchen
authorhegyi
Thu, 17 Mar 2005 17:20:37 +0000
changeset 12254401e1aeafcf
parent 1224 7f4f2855fa11
child 1226 71fcebd3a041
Magic anyangle is Faster, harder, Blumchen
src/work/peter/Makefile
src/work/peter/canvas-test.cc
     1.1 --- a/src/work/peter/Makefile	Thu Mar 17 11:45:05 2005 +0000
     1.2 +++ b/src/work/peter/Makefile	Thu Mar 17 17:20:37 2005 +0000
     1.3 @@ -1,2 +1,7 @@
     1.4  triangle: canvas-test.cc
     1.5  	g++ canvas-test.cc -W -Wall -ansi -pedantic -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs`
     1.6 +
     1.7 +run: triangle
     1.8 +	./triangle 100 100 -100 100 0 -100
     1.9 +
    1.10 +.PHONY: run
     2.1 --- a/src/work/peter/canvas-test.cc	Thu Mar 17 11:45:05 2005 +0000
     2.2 +++ b/src/work/peter/canvas-test.cc	Thu Mar 17 17:20:37 2005 +0000
     2.3 @@ -18,10 +18,10 @@
     2.4  private:
     2.5  
     2.6  	///Event handler function that handles dragging nodes of triangle
     2.7 -	bool event_handler(GdkEvent* e, bool b);
     2.8 +	bool event_handler(GdkEvent* e, int b);
     2.9  
    2.10  	///Event handler function that handles dragging triangle
    2.11 -	bool tri_mover(GdkEvent* e, bool b);
    2.12 +	bool tri_mover(GdkEvent* e);
    2.13  
    2.14  	///Coordinates of Weight Point of tirangle
    2.15  	Gnome::Art::Point * wp;
    2.16 @@ -38,6 +38,9 @@
    2.17  	///Number Of Elements - the number of nodes
    2.18  	int noe;
    2.19  
    2.20 +	///Array of coordinates
    2.21 +	double * coordinates;
    2.22 +
    2.23  	///At this location was the mousebutton pressed.
    2.24  	///It helps to calculate the distance of dragging.
    2.25  	double clicked_x, clicked_y;
    2.26 @@ -52,9 +55,8 @@
    2.27  };
    2.28  
    2.29  ///When we click on the weight point we can drag the whole triangle. This function resolves it.
    2.30 -bool CanvasExample::tri_mover(GdkEvent* e, bool b)
    2.31 +bool CanvasExample::tri_mover(GdkEvent* e)
    2.32  {
    2.33 -	b=b;
    2.34  	switch(e->type)
    2.35  	{
    2.36  		case GDK_BUTTON_PRESS:
    2.37 @@ -71,24 +73,24 @@
    2.38  			{
    2.39  				double dx=e->motion.x-clicked_x;
    2.40  				double dy=e->motion.y-clicked_y;
    2.41 +
    2.42 +				Gnome::Canvas::Points coos;
    2.43 +
    2.44  				for(int i=0;i<=noe;i++)
    2.45  				{
    2.46  					nodes[i]->move(dx,dy);
    2.47 +
    2.48 +					double x=(coordinates[2*i]+=dx);
    2.49 +					double y=(coordinates[2*i+1]+=dy);
    2.50 +
    2.51 +					if(i!=noe)coos.push_back(Gnome::Art::Point(x,y));
    2.52 +
    2.53  				}
    2.54 +
    2.55  				clicked_x=e->motion.x;
    2.56  				clicked_y=e->motion.y;
    2.57  
    2.58 -				Gnome::Canvas::Points coos;
    2.59 -				for(int i=0;i<noe;i++)
    2.60 -				{
    2.61 -					double x1,y1,x2,y2;
    2.62 -					nodes[i]->get_bounds(x1,y1,x2,y2);
    2.63 -					double x=(x1+x2)/2;
    2.64 -					double y=(y1+y2)/2;
    2.65 -					coos.push_back(Gnome::Art::Point(x, y));
    2.66 -				}
    2.67  				sides->property_points().set_value(coos);
    2.68 -
    2.69  			}
    2.70  		default: break;
    2.71  	}
    2.72 @@ -98,9 +100,8 @@
    2.73  ///This function moves only one node of triangle,
    2.74  ///but recalculate the location of wight point,
    2.75  ///and also redraw the sides of the planefigure.
    2.76 -bool CanvasExample::event_handler(GdkEvent* e, bool b)
    2.77 +bool CanvasExample::event_handler(GdkEvent* e, int b)
    2.78  {
    2.79 -	b=b;
    2.80  	switch(e->type)
    2.81  	{
    2.82  		case GDK_BUTTON_PRESS:
    2.83 @@ -124,6 +125,9 @@
    2.84  				double dy=e->motion.y-clicked_y;
    2.85  				active_item->move(dx, dy);
    2.86  
    2.87 +				coordinates[2*b]+=dx;
    2.88 +				coordinates[2*b+1]+=dy;
    2.89 +
    2.90  				Gnome::Canvas::Points coos;
    2.91  
    2.92  				double x_wp=0;
    2.93 @@ -131,14 +135,10 @@
    2.94  
    2.95  				for(int i=0;i<noe;i++)
    2.96  				{
    2.97 -					double x1,y1,x2,y2;
    2.98 -					nodes[i]->get_bounds(x1,y1,x2,y2);
    2.99 -					double x=(x1+x2)/2;
   2.100 -					double y=(y1+y2)/2;
   2.101 -					coos.push_back(Gnome::Art::Point(x, y));
   2.102 +					coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
   2.103  
   2.104 -					x_wp+=x;
   2.105 -					y_wp+=y;
   2.106 +					x_wp+=coordinates[2*i];
   2.107 +					y_wp+=coordinates[2*i+1];
   2.108  				}
   2.109  
   2.110  				sides->property_points().set_value(coos);
   2.111 @@ -146,14 +146,12 @@
   2.112  				x_wp/=noe;
   2.113  				y_wp/=noe;
   2.114  
   2.115 -				double x1,y1,x2,y2;
   2.116 -				nodes[noe]->get_bounds(x1,y1,x2,y2);
   2.117 -				double x=(x1+x2)/2;
   2.118 -				double y=(y1+y2)/2;
   2.119 +				dx=x_wp-coordinates[noe*2];
   2.120 +				dy=y_wp-coordinates[noe*2+1];
   2.121 +				nodes[noe]->move(dx, dy);
   2.122  
   2.123 -				dx=x_wp-x;
   2.124 -				dy=y_wp-y;
   2.125 -				nodes[noe]->move(dx, dy);
   2.126 +				coordinates[noe*2]+=dx;
   2.127 +				coordinates[noe*2+1]+=dy;
   2.128  
   2.129  				clicked_x=e->motion.x;
   2.130  				clicked_y=e->motion.y;
   2.131 @@ -167,18 +165,20 @@
   2.132  {
   2.133  	noe=numofcoos/2;
   2.134  
   2.135 +	coordinates=new double [numofcoos+2];
   2.136 +
   2.137  	double x_wp=0;
   2.138  	double y_wp=0;
   2.139  
   2.140  	Gnome::Canvas::Points coos;
   2.141  	for(int i=0;i<numofcoos;i++)
   2.142  	{
   2.143 -		double x=coosarray[i++];
   2.144 -		double y=coosarray[i];
   2.145 -		coos.push_back(Gnome::Art::Point(x, y));
   2.146 +		coordinates[i]=coosarray[i++];
   2.147 +		coordinates[i]=coosarray[i];
   2.148 +		coos.push_back(Gnome::Art::Point(coordinates[i-1], coordinates[i]));
   2.149  
   2.150 -		x_wp+=x;
   2.151 -		y_wp+=y;
   2.152 +		x_wp+=coordinates[i-1];
   2.153 +		y_wp+=coordinates[i];
   2.154  
   2.155  	}
   2.156  
   2.157 @@ -193,15 +193,26 @@
   2.158  		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.159  		*(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
   2.160  		*(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
   2.161 -		(nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true));
   2.162 +		(nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),i));
   2.163  	}
   2.164  
   2.165 -	wp=new Gnome::Art::Point(x_wp/noe,y_wp/noe);
   2.166 +	coordinates[numofcoos]=x_wp/noe;
   2.167 +	coordinates[numofcoos+1]=y_wp/noe;
   2.168  
   2.169 -	nodes[noe]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
   2.170 +	wp=new Gnome::Art::Point(coordinates[numofcoos],coordinates[numofcoos+1]);
   2.171 +
   2.172 +	nodes[noe]= new Gnome::Canvas::Ellipse
   2.173 +	(
   2.174 +		triangle,
   2.175 +		coordinates[numofcoos]-20,
   2.176 +		coordinates[numofcoos+1]-20,
   2.177 +		coordinates[numofcoos]+20,
   2.178 +		coordinates[numofcoos+1]+20
   2.179 +	);
   2.180  	*(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
   2.181  	*(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
   2.182 -	(nodes[noe])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
   2.183 +	(nodes[noe])->signal_event().connect(sigc::mem_fun(*this, &CanvasExample::tri_mover));
   2.184 +
   2.185  
   2.186  
   2.187  }