5 Edge::Edge(Vertex
&source
, Vertex
&target
) :
18 void Edge::draw(Cairo::RefPtr
<Cairo::Context
> cr
)
20 gdouble start_x
, start_y
;
24 _source
->get_bottom_anchor(start_x
, start_y
);
25 _target
->get_top_anchor(end_x
, end_y
);
29 cr
->move_to(start_x
, start_y
);
30 cr
->line_to(end_x
, end_y
);
31 cr
->set_line_width(2.0);
32 cr
->set_line_cap(Cairo::LINE_CAP_ROUND
);
38 bool Edge::select(Cairo::RefPtr
<Cairo::Context
> cr
, gdouble x
, gdouble y
)
40 if (!_is_in_region(cr
, x
, y
)) {
44 // If y-coordinate is positive, it means that it is closer to the target
47 change_target_follow_mouse();
49 change_source_follow_mouse();
55 void Edge::deselect(void)
60 void Edge::change_source(Vertex
&source
)
65 void Edge::change_target(Vertex
&target
)
70 void Edge::change_source_follow_mouse(void)
75 void Edge::change_target_follow_mouse(void)
81 Edge::_is_in_region(Cairo::RefPtr
<Cairo::Context
> cr
, gdouble x
, gdouble y
) const
83 gdouble start_x
, start_y
;
85 gdouble a
, b
, distance
;
87 _source
->get_bottom_anchor(start_x
, start_y
);
88 _target
->get_top_anchor(end_x
, end_y
);
90 // Discards the point if its higher than the source anchor or lower than
92 if (y
< start_y
|| y
> end_y
) {
96 // The point has to be within 5 units away from the line segment
99 distance
= (fabs(a
* (end_y
- y
) - (end_x
- x
) * b
) /
100 sqrt(pow(a
, 2) + pow(b
, 2)));
101 if (distance
< 5.0) {