ladanyi@53: #include "broken_edge.h" alpar@59: #include hegyi@17: hegyi@21: BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false) hegyi@17: { hegyi@19: my_points=new Gnome::Art::Point[3]; hegyi@19: hegyi@19: arrow=new Gnome::Canvas::Polygon(g); hegyi@19: *arrow << Gnome::Canvas::Properties::fill_color("red"); hegyi@30: arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edgeFormerEventHandler)); ladanyi@63: arrow->lower_to_bottom(); hegyi@30: setPoints(p); hegyi@19: } hegyi@19: hegyi@19: BrokenEdge::~BrokenEdge() hegyi@19: { hegyi@19: if(arrow)delete(arrow); hegyi@19: } hegyi@19: hegyi@30: void BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move) hegyi@19: { hegyi@19: bool set_arrow=false; hegyi@50: //red arrow losts its position-right button hegyi@20: if(!move) hegyi@17: { hegyi@20: if(p.size()==2) hegyi@20: { hegyi@20: set_arrow=true; hegyi@20: Gnome::Canvas::Points points_with_center; hegyi@20: points_with_center.push_back(my_points[0]=p[0]); hegyi@20: 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 ); hegyi@20: points_with_center.push_back(my_points[2]=p[1]); hegyi@20: property_points().set_value(points_with_center); hegyi@20: } hegyi@20: if(p.size()==3) hegyi@20: { hegyi@20: set_arrow=true; hegyi@20: property_points().set_value(p); hegyi@20: for(int i=0;i<3;i++) hegyi@20: { hegyi@20: my_points[i]=p[i]; hegyi@20: } hegyi@20: } hegyi@20: } hegyi@20: else hegyi@19: { hegyi@50: //arrow keeps its position-left button hegyi@50: hegyi@51: if(p.size()==2) hegyi@51: { hegyi@51: Gnome::Canvas::Points points; hegyi@51: my_points[0]=p[0]; hegyi@51: my_points[2]=p[1]; hegyi@51: for(int i=0;i<3;i++) hegyi@51: { hegyi@51: points.push_back(my_points[i]); hegyi@51: } hegyi@51: property_points().set_value(points); hegyi@51: } hegyi@51: set_arrow=true; hegyi@50: hegyi@50: ////////////////////////////////////////////////////////////////////////////////////////////////////// hegyi@50: /////////// kepps shape-with angles hegyi@50: ////////////////////////////////////////////////////////////////////////////////////////////////////// hegyi@50: hegyi@50: hegyi@50: // //old vector from one to the other node hegyi@50: // xy o_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y()); hegyi@50: // //projection of the old vector to positive x axis hegyi@50: // xy o_x_p2p(fabs(o_p2p.x),0); hegyi@50: // //length of p2p vector hegyi@50: // double o_l_p2p=sqrt( o_p2p.normSquare() ); hegyi@50: // if(o_p2p.x<0) hegyi@50: // { hegyi@50: // o_l_p2p*=-1; hegyi@50: // } hegyi@50: // //length of projection of p2p vector hegyi@50: // double o_l_x_p2p=sqrt( o_x_p2p.normSquare() ); hegyi@50: // //old angle of p2p vector to the x axis hegyi@50: // double o_a_p2p=acos(o_l_x_p2p/o_l_p2p); hegyi@50: // if(o_p2p.y>0) hegyi@50: // { hegyi@50: // o_a_p2p=2*M_PI-o_a_p2p; hegyi@50: // } hegyi@50: hegyi@50: // //old vector from first node to the breakpoint hegyi@50: // xy o_1b((my_points[1].get_x()-my_points[0].get_x()),(my_points[1].get_y()-my_points[0].get_y())); hegyi@50: // //projection of the old node-breakpoint vector to positive x axis hegyi@50: // xy o_x_1b(fabs(o_1b.x),0); hegyi@50: // //length of 1b vector hegyi@50: // double o_l_1b=sqrt( o_1b.normSquare() ); hegyi@50: // if(o_1b.x<0) hegyi@50: // { hegyi@50: // o_l_1b*=-1; hegyi@50: // } hegyi@50: // //length of projection of 1b vector hegyi@50: // double o_l_x_1b=sqrt( o_x_1b.normSquare() ); hegyi@50: // //old angle of 1b vector to the x axis hegyi@50: // double o_a_1b=acos(o_l_x_1b/o_l_1b); hegyi@50: // if(o_1b.y>0) hegyi@50: // { hegyi@50: // o_a_1b=2*M_PI-o_a_1b; hegyi@50: // } hegyi@50: hegyi@50: // if(p.size()==2) hegyi@50: // { hegyi@50: // set_arrow=true; hegyi@50: hegyi@50: // my_points[0]=p[0]; hegyi@50: // my_points[2]=p[1]; hegyi@50: hegyi@50: // //new vector from one to the other node hegyi@50: // xy n_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y()); hegyi@50: // //projection of the new vector to positive x axis hegyi@50: // xy n_x_p2p(fabs(n_p2p.x),0); hegyi@50: // //length of p2p vector hegyi@50: // double n_l_p2p=sqrt( n_p2p.normSquare() ); hegyi@50: // if(n_p2p.x<0) hegyi@50: // { hegyi@50: // n_l_p2p*=-1; hegyi@50: // } hegyi@50: // //length of projection of p2p vector hegyi@50: // double n_l_x_p2p=sqrt( n_x_p2p.normSquare() ); hegyi@50: // //new angle of p2p vector to the x axis hegyi@50: // double n_a_p2p=acos(n_l_x_p2p/n_l_p2p); hegyi@50: // if(n_p2p.y>0) hegyi@50: // { hegyi@50: // n_a_p2p=2*M_PI-n_a_p2p; hegyi@50: // } hegyi@50: hegyi@50: // //new angle of 1b vector to the x axis hegyi@50: // double n_a_1b=o_a_1b+n_a_p2p-o_a_p2p; hegyi@50: hegyi@50: // 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; hegyi@50: // 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; hegyi@50: hegyi@50: // // std::cout << o_p2p << " " << n_p2p << std::endl; hegyi@50: hegyi@50: // if((n_a_1b>M_PI*3/2)||(n_a_1bM_PI/2)) hegyi@50: // { hegyi@50: // std::cout << "bal terfel" << std::endl; hegyi@50: // 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); hegyi@50: // } hegyi@50: // else hegyi@50: // { hegyi@50: // std::cout << "y tengely" << std::endl; hegyi@50: // double new_y=my_points[1].get_y(); hegyi@50: // 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); hegyi@50: // } hegyi@50: hegyi@50: // 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; hegyi@50: hegyi@50: hegyi@50: // // if(o_1b.x*o_1b.y>0) hegyi@50: // // { hegyi@50: // // if(n_p2p.x>0) hegyi@50: // // { hegyi@50: // // 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); hegyi@50: // // } hegyi@50: // // else hegyi@50: // // { hegyi@50: // // 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); hegyi@50: // // } hegyi@50: // // } hegyi@50: // // else if(o_1b.x*o_1b.y<0) hegyi@50: // // { hegyi@50: // // if(n_p2p.x>0) hegyi@50: // // { hegyi@50: // // 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); hegyi@50: // // } hegyi@50: // // else hegyi@50: // // { hegyi@50: // // 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); hegyi@50: // // } hegyi@50: // // } hegyi@50: // // else hegyi@50: // // { hegyi@50: // // } hegyi@50: hegyi@50: ////////////////////////////////////////////////////////////////////////////////////////////////////// hegyi@50: /////////// kepps shape-with scalar multiplication hegyi@50: ////////////////////////////////////////////////////////////////////////////////////////////////////// hegyi@50: hegyi@51: // if(p.size()==2) hegyi@51: // { hegyi@51: // //old vector from one to the other node - a hegyi@51: // xy a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y()); hegyi@51: // //new vector from one to the other node - b hegyi@51: // xy b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y()); hegyi@50: hegyi@51: // //old vector from one node to the breakpoint - c hegyi@51: // xy c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_y()); hegyi@50: hegyi@51: // //new vector from one node to the breakpoint - d - we have to calculate this one hegyi@51: // xy d_v; hegyi@50: hegyi@51: // //scalar product of a and b (old and new vector from first point to the other) hegyi@51: // double sab=a_v*b_v; hegyi@51: // //scalar product of c and d (old and new vector from first point to breakpoint) hegyi@51: // double scd=sab*c_v.normSquare()/a_v.normSquare(); hegyi@50: hegyi@51: // std::cout<<" a " << a_v<<" b " < target( my_points[2].get_x(), my_points[2].get_y() ); hegyi@19: xy center( my_points[1].get_x(), my_points[1].get_y() ); hegyi@19: hegyi@19: xy unit_vector_in_dir(target-center); hegyi@19: // std::cout << target << " - " << center << " = " << unit_vector_in_dir << " / " <property_points().set_value(arrow_points); hegyi@17: } hegyi@17: } hegyi@19: hegyi@30: bool BrokenEdge::edgeFormerEventHandler(GdkEvent* e) hegyi@19: { hegyi@19: switch(e->type) hegyi@19: { hegyi@19: case GDK_BUTTON_PRESS: hegyi@19: //we mark the location of the event to be able to calculate parameters of dragging hegyi@30: if(gdc.getActualTool()!=CREATE_NODE) hegyi@21: { hegyi@30: gdc.toggleEdgeActivity(this, true); hegyi@21: clicked_x=e->button.x; hegyi@21: clicked_y=e->button.y; hegyi@21: isbutton=true; hegyi@21: } hegyi@19: break; hegyi@19: case GDK_BUTTON_RELEASE: hegyi@30: if(gdc.getActualTool()!=CREATE_NODE) hegyi@25: { hegyi@30: gdc.toggleEdgeActivity(this, false); hegyi@25: isbutton=false; hegyi@25: } hegyi@19: break; hegyi@19: case GDK_MOTION_NOTIFY: hegyi@19: //we only have to do sg. if the mouse button is pressed hegyi@19: if(isbutton) hegyi@19: { hegyi@19: //new coordinates will be the old values, hegyi@19: //because the item will be moved to the hegyi@19: //new coordinate therefore the new movement hegyi@19: //has to be calculated from here hegyi@19: hegyi@19: double dx=e->motion.x-clicked_x; hegyi@19: double dy=e->motion.y-clicked_y; hegyi@19: hegyi@19: Gnome::Canvas::Points points_new; hegyi@19: hegyi@19: points_new.push_back(my_points[0]); hegyi@19: points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy)); hegyi@19: points_new.push_back(my_points[2]); hegyi@19: hegyi@30: setPoints(points_new); hegyi@30: gdc.textReposition(xy(my_points[1].get_x(),my_points[1].get_y())); hegyi@19: hegyi@19: clicked_x=e->motion.x; hegyi@19: clicked_y=e->motion.y; hegyi@19: hegyi@19: } hegyi@19: default: break; hegyi@19: } hegyi@19: hegyi@19: return true; hegyi@19: } hegyi@25: hegyi@30: xy BrokenEdge::getArrowPos() hegyi@25: { hegyi@25: xy ret_val(my_points[1].get_x(),my_points[1].get_y()); hegyi@25: return ret_val; hegyi@25: }