]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/object.cpp
Make use of the mspmath library
[r2c2.git] / source / libr2c2 / object.cpp
1 #include <stdexcept>
2 #include "object.h"
3
4 using namespace std;
5
6 namespace R2C2 {
7
8 Snap Object::get_snap_node(unsigned) const
9 {
10         throw out_of_range("Object::get_snap_node");
11 }
12
13 bool Object::snap(Snap &sn, float limit, SnapType what) const
14 {
15         if(what&SNAP_NODE)
16         {
17                 unsigned nsn = get_n_snap_nodes();
18                 for(unsigned i=0; i<nsn; ++i)
19                 {
20                         Snap node = get_snap_node(i);
21                         Vector span = sn.position-node.position;
22                         if(dot(span, span)<limit*limit)
23                         {
24                                 sn = node;
25                                 return true;
26                         }
27                 }
28         }
29
30         return false;
31 }
32
33 bool Object::snap_to(const Object &other, float limit, SnapType what)
34 {
35         if(what==SNAP_DEFAULT)
36                 what = get_default_snap_type_to(other);
37
38         if(!what)
39                 return false;
40
41         unsigned nsn = get_n_snap_nodes();
42         for(unsigned i=0; i<nsn; ++i)
43         {
44                 Snap sn = get_snap_node(i);
45                 Snap ssn = sn;
46                 if(other.snap(ssn, limit, what))
47                 {
48                         set_rotation(rotation+ssn.rotation-sn.rotation-Angle::half_turn());
49                         sn = get_snap_node(i);
50                         position += ssn.position-sn.position;
51                         return true;
52                 }
53         }
54
55         return false;
56 }
57
58 Object *Object::get_link(unsigned) const
59 {
60         throw out_of_range("Object::get_link");
61 }
62
63 bool Object::break_link(Object &other)
64 {
65         unsigned nls = get_n_link_slots();
66         for(unsigned i=0; i<nls; ++i)
67                 if(get_link(i)==&other)
68                         return break_link(i);
69
70         return false;
71 }
72
73 void Object::break_links()
74 {
75         unsigned nls = get_n_link_slots();
76         for(unsigned i=0; i<nls; ++i)
77                 break_link(i);
78 }
79
80 } // namespace R2C2