1 #define __LINE_GEOMETRY_C__
3 /*
4 * Routines for dealing with lines (intersections, etc.)
5 *
6 * Authors:
7 * Maximilian Albert <Anhalter42@gmx.de>
8 *
9 * Copyright (C) 2007 authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "line-geometry.h"
15 #include "inkscape.h"
16 #include "desktop-style.h"
17 #include "desktop-handles.h"
18 #include "display/sp-canvas.h"
19 #include "display/sodipodi-ctrl.h"
20 //#include "display/curve.cpp"
22 namespace Box3D {
24 /**
25 * Draw a line beginning at 'start'. If is_endpoint is true, use 'vec' as the endpoint
26 * of the segment. Otherwise interpret it as the direction of the line.
27 * FIXME: Think of a better way to distinguish between the two constructors of lines.
28 */
29 Line::Line(NR::Point const &start, NR::Point const &vec, bool is_endpoint) {
30 pt = start;
31 if (is_endpoint)
32 v_dir = vec - start;
33 else
34 v_dir = vec;
35 normal = v_dir.ccw();
36 d0 = NR::dot(normal, pt);
37 }
39 Line::Line(Line const &line) {
40 pt = line.pt;
41 v_dir = line.v_dir;
42 normal = line.normal;
43 d0 = line.d0;
44 }
46 Line &Line::operator=(Line const &line) {
47 pt = line.pt;
48 v_dir = line.v_dir;
49 normal = line.normal;
50 d0 = line.d0;
52 return *this;
53 }
55 NR::Maybe<NR::Point> Line::intersect(Line const &line) {
56 NR::Coord denom = NR::dot(v_dir, line.normal);
57 g_return_val_if_fail(fabs(denom) > 1e-6, NR::Nothing());
59 NR::Coord lambda = (line.d0 - NR::dot(pt, line.normal)) / denom;
60 return pt + lambda * v_dir;
61 }
63 void Line::set_direction(NR::Point const &dir)
64 {
65 v_dir = dir;
66 normal = v_dir.ccw();
67 d0 = NR::dot(normal, pt);
68 }
70 NR::Point Line::closest_to(NR::Point const &pt)
71 {
72 /* return the intersection of this line with a perpendicular line passing through pt */
73 NR::Maybe<NR::Point> result = this->intersect(Line(pt, (this->v_dir).ccw(), false));
74 g_return_val_if_fail (result, NR::Point (0.0, 0.0));
75 return *result;
76 }
78 void create_canvas_point(NR::Point const &pos, double size, guint32 rgba)
79 {
80 SPDesktop *desktop = inkscape_active_desktop();
81 SPCanvasItem * canvas_pt = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRL,
82 "size", size,
83 "filled", 1,
84 "fill_color", rgba,
85 "stroked", 1,
86 "stroke_color", 0x000000ff,
87 NULL);
88 SP_CTRL(canvas_pt)->moveto(pos);
89 }
91 void create_canvas_line(NR::Point const &p1, NR::Point const &p2, guint32 rgba)
92 {
93 SPDesktop *desktop = inkscape_active_desktop();
94 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(desktop),
95 SP_TYPE_CTRLLINE, NULL);
96 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
97 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
98 sp_canvas_item_show (line);
99 }
101 } // namespace Box3D
103 /*
104 Local Variables:
105 mode:c++
106 c-file-style:"stroustrup"
107 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
108 indent-tabs-mode:nil
109 fill-column:99
110 End:
111 */
112 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :