1 #include "broken_edge.h"
4 BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false)
6 my_points=new Gnome::Art::Point[3];
8 arrow=new Gnome::Canvas::Polygon(g);
9 *arrow << Gnome::Canvas::Properties::fill_color("red");
10 arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edgeFormerEventHandler));
14 BrokenEdge::~BrokenEdge()
16 if(arrow)delete(arrow);
19 void BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
22 //red arrow losts its position-right button
28 Gnome::Canvas::Points points_with_center;
29 points_with_center.push_back(my_points[0]=p[0]);
30 points_with_center.push_back(my_points[1]=Gnome::Art::Point( (p[0].get_x()+p[1].get_x())/2+0 , (p[0].get_y()+p[1].get_y())/2 )+0 );
31 points_with_center.push_back(my_points[2]=p[1]);
32 property_points().set_value(points_with_center);
37 property_points().set_value(p);
46 //arrow keeps its position-left button
50 Gnome::Canvas::Points points;
55 points.push_back(my_points[i]);
57 property_points().set_value(points);
61 //////////////////////////////////////////////////////////////////////////////////////////////////////
62 /////////// kepps shape-with angles
63 //////////////////////////////////////////////////////////////////////////////////////////////////////
66 // //old vector from one to the other node
67 // xy<double> o_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
68 // //projection of the old vector to positive x axis
69 // xy<double> o_x_p2p(fabs(o_p2p.x),0);
70 // //length of p2p vector
71 // double o_l_p2p=sqrt( o_p2p.normSquare() );
76 // //length of projection of p2p vector
77 // double o_l_x_p2p=sqrt( o_x_p2p.normSquare() );
78 // //old angle of p2p vector to the x axis
79 // double o_a_p2p=acos(o_l_x_p2p/o_l_p2p);
82 // o_a_p2p=2*M_PI-o_a_p2p;
85 // //old vector from first node to the breakpoint
86 // xy<double> o_1b((my_points[1].get_x()-my_points[0].get_x()),(my_points[1].get_y()-my_points[0].get_y()));
87 // //projection of the old node-breakpoint vector to positive x axis
88 // xy<double> o_x_1b(fabs(o_1b.x),0);
89 // //length of 1b vector
90 // double o_l_1b=sqrt( o_1b.normSquare() );
95 // //length of projection of 1b vector
96 // double o_l_x_1b=sqrt( o_x_1b.normSquare() );
97 // //old angle of 1b vector to the x axis
98 // double o_a_1b=acos(o_l_x_1b/o_l_1b);
101 // o_a_1b=2*M_PI-o_a_1b;
108 // my_points[0]=p[0];
109 // my_points[2]=p[1];
111 // //new vector from one to the other node
112 // xy<double> n_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
113 // //projection of the new vector to positive x axis
114 // xy<double> n_x_p2p(fabs(n_p2p.x),0);
115 // //length of p2p vector
116 // double n_l_p2p=sqrt( n_p2p.normSquare() );
121 // //length of projection of p2p vector
122 // double n_l_x_p2p=sqrt( n_x_p2p.normSquare() );
123 // //new angle of p2p vector to the x axis
124 // double n_a_p2p=acos(n_l_x_p2p/n_l_p2p);
127 // n_a_p2p=2*M_PI-n_a_p2p;
130 // //new angle of 1b vector to the x axis
131 // double n_a_1b=o_a_1b+n_a_p2p-o_a_p2p;
133 // std::cout << " p2p regi: " << o_a_p2p/M_PI*180 << " uj: " << n_a_p2p/M_PI*180-(int)n_a_p2p/M_PI*180 << std::endl;
134 // std::cout << " 1b regi: " << o_a_1b/M_PI*180 << " uj: " << n_a_1b/M_PI*180-(int)n_a_1b/M_PI*180 << std::endl;
136 // // std::cout << o_p2p << " " << n_p2p << std::endl;
138 // if((n_a_1b>M_PI*3/2)||(n_a_1b<M_PI/2))
140 // std::cout << "jobb terfel" << std::endl;
141 // my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
143 // else if((n_a_1b<M_PI*3/2)&&(n_a_1b>M_PI/2))
145 // std::cout << "bal terfel" << std::endl;
146 // my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
150 // std::cout << "y tengely" << std::endl;
151 // double new_y=my_points[1].get_y();
152 // my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,new_y);
155 // std::cout << "alap: " << p[0] << " eredeti hossz: " << o_l_1b << " nagy uj: " << n_l_p2p << " nagy regi: " << o_l_p2p << " a*b/c " << o_l_1b*n_l_p2p/o_l_p2p << " eredmeny: " << my_points[1] << std::endl;
158 // // if(o_1b.x*o_1b.y>0)
162 // // my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
166 // // my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
169 // // else if(o_1b.x*o_1b.y<0)
173 // // my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
177 // // my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
184 //////////////////////////////////////////////////////////////////////////////////////////////////////
185 /////////// kepps shape-with scalar multiplication
186 //////////////////////////////////////////////////////////////////////////////////////////////////////
190 // //old vector from one to the other node - a
191 // xy<double> a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
192 // //new vector from one to the other node - b
193 // xy<double> b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y());
195 // //old vector from one node to the breakpoint - c
196 // xy<double> c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_y());
198 // //new vector from one node to the breakpoint - d - we have to calculate this one
201 // //scalar product of a and b (old and new vector from first point to the other)
202 // double sab=a_v*b_v;
203 // //scalar product of c and d (old and new vector from first point to breakpoint)
204 // double scd=sab*c_v.normSquare()/a_v.normSquare();
206 // std::cout<<" a " << a_v<<" b " <<b_v<<" c " <<c_v<<" sab " <<sab<<" scd "<<scd<<std::endl;
208 // double a=c_v.normSquare();
209 // double b=2*scd*c_v.y;
210 // double c=scd*scd-b_v.normSquare()/a_v.normSquare()*c_v.normSquare()*c_v.x*c_v.x;
212 // std::cout<<" a " << a<<" b " <<b<<" c " <<c<<std::endl;
214 // d_v.y=(-b-sqrt(b*b-4*a*c))/2/a;
218 // d_v.x=(scd-c_v.y*d_v.y)/c_v.x;
222 // d_v.x=my_points[1].get_x();
225 // std::cout<<" d " << d_v<<std::endl;
227 // my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y());
229 // Gnome::Canvas::Points points;
230 // for(int i=0;i<3;i++)
232 // points.push_back(my_points[i]);
234 // property_points().set_value(points);
239 //calculating coordinates of the direction indicator arrow
241 xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
242 xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
244 xy<gdouble> unit_vector_in_dir(target-center);
245 // std::cout << target << " - " << center << " = " << unit_vector_in_dir << " / " <<unit_vector_in_dir.normSquare() ;
246 unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
247 // std::cout << " = " << unit_vector_in_dir << std::endl;
249 xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
250 // std::cout << unit_norm_vector << std::endl;
255 // - - // c(enter)l(eft), ccl, ccr, cr
257 // || // b(ottom)l, br
262 xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
263 xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
264 xy<gdouble> ccl(center + unit_vector_in_dir * size + unit_norm_vector * size );
265 xy<gdouble> ccr(center + unit_vector_in_dir * size - unit_norm_vector * size );
266 xy<gdouble> cl (center + unit_vector_in_dir * size + unit_norm_vector * 2 * size );
267 xy<gdouble> cr (center + unit_vector_in_dir * size - unit_norm_vector * 2 * size );
268 xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
270 // std::cout << bl << " " << br << " " << ccl << " " << ccr << " " << cl << " " << cr << " " << top << std::endl;
272 Gnome::Canvas::Points arrow_points;
273 arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y ) );
274 arrow_points.push_back(Gnome::Art::Point( br.x , br.y ) );
275 arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
276 arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y ) );
277 arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
278 arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y ) );
279 arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
281 arrow->property_points().set_value(arrow_points);
285 bool BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
289 case GDK_BUTTON_PRESS:
290 //we mark the location of the event to be able to calculate parameters of dragging
291 if(gdc.getActualTool()!=CREATE_NODE)
293 gdc.toggleEdgeActivity(this, true);
294 clicked_x=e->button.x;
295 clicked_y=e->button.y;
299 case GDK_BUTTON_RELEASE:
300 if(gdc.getActualTool()!=CREATE_NODE)
302 gdc.toggleEdgeActivity(this, false);
306 case GDK_MOTION_NOTIFY:
307 //we only have to do sg. if the mouse button is pressed
310 //new coordinates will be the old values,
311 //because the item will be moved to the
312 //new coordinate therefore the new movement
313 //has to be calculated from here
315 double dx=e->motion.x-clicked_x;
316 double dy=e->motion.y-clicked_y;
318 Gnome::Canvas::Points points_new;
320 points_new.push_back(my_points[0]);
321 points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
322 points_new.push_back(my_points[2]);
324 setPoints(points_new);
325 gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));
327 clicked_x=e->motion.x;
328 clicked_y=e->motion.y;
337 xy<double> BrokenEdge::getArrowPos()
339 xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());