]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/trackpart.cpp
Code reformatting: add spaces around assignment operators
[r2c2.git] / source / libmarklin / trackpart.cpp
index 90bc30c02b5bc105f8e537a41005f44b390ab026..943832fff413577dc77eabdef5d7d16840d61dbc 100644 (file)
@@ -1,3 +1,10 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
 #include <cmath>
 #include "trackpart.h"
 
@@ -6,8 +13,6 @@ using namespace std;
 namespace Marklin {
 
 TrackPart::TrackPart():
-       x(0),
-       y(0),
        dir(0),
        length(0),
        radius(0),
@@ -15,28 +20,40 @@ TrackPart::TrackPart():
        dead_end(false)
 { }
 
-void TrackPart::collect_endpoints(vector<Endpoint> &eps)
+void TrackPart::collect_endpoints(vector<Endpoint> &eps) const
 {
-       eps.push_back(Endpoint(x, y, dir+M_PI, 1<<route));
+       eps.push_back(Endpoint(pos.x, pos.y, dir+M_PI, 1<<route));
 
        if(dead_end)
                ;
        else if(radius)
        {
-               float a=((radius<0) ? -length : length);
-               float c=cos(a);
-               float s=sin(a);
-               float rx=radius*sin(dir);
-               float ry=-radius*cos(dir);
-               eps.push_back(Endpoint(x+c*rx-s*ry-rx, y+c*ry+s*rx-ry, dir+a, 1<<route));
+               float a = ((radius<0) ? -length : length);
+               Point p = get_point(length*abs(radius));
+               eps.push_back(Endpoint(p.x, p.y, dir+a, 1<<route));
+       }
+       else
+               eps.push_back(Endpoint(pos.x+cos(dir)*length, pos.y+sin(dir)*length, dir, 1<<route));
+}
+
+Point TrackPart::get_point(float d) const
+{
+       if(radius)
+       {
+               float a = d/radius;
+               float c = cos(a);
+               float s = sin(a);
+               float rx = radius*sin(dir);
+               float ry = -radius*cos(dir);
+               return Point(pos.x+c*rx-s*ry-rx, pos.y+c*ry+s*rx-ry);
        }
        else
-               eps.push_back(Endpoint(x+cos(dir)*length, y+sin(dir)*length, dir, 1<<route));
+               return Point(pos.x+cos(dir)*d, pos.y+sin(dir)*d);
 }
 
 
 TrackPart::Loader::Loader(TrackPart &p):
-       part(p)
+       Msp::DataFile::BasicLoader<TrackPart>(p)
 {
        add("start",    &Loader::start);
        add("length",   &TrackPart::length);
@@ -47,24 +64,23 @@ TrackPart::Loader::Loader(TrackPart &p):
 
 void TrackPart::Loader::finish()
 {
-       if(part.radius)
+       if(obj.radius)
        {
-               part.length*=M_PI/180;
-               part.radius/=1000;
+               obj.length *= M_PI/180;
+               obj.radius /= 1000;
        }
        else
-               part.length/=1000;
+               obj.length /= 1000;
 
-       part.x/=1000;
-       part.y/=1000;
-       part.dir*=M_PI/180;
+       obj.pos.x /= 1000;
+       obj.pos.y /= 1000;
+       obj.dir *= M_PI/180;
 }
 
 void TrackPart::Loader::start(float x, float y, float d)
 {
-       part.x=x;
-       part.y=y;
-       part.dir=d;
+       obj.pos = Point(x, y);
+       obj.dir = d;
 }
 
 } // namespace Marklin