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