-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock.h
More file actions
143 lines (126 loc) · 3.99 KB
/
Copy pathclock.h
File metadata and controls
143 lines (126 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <string>
#include <deque>
#include <math.h>
#include <stdlib.h>
#ifndef _CLOCK_H_
#define _CLOCK_H_
#define TOLERANCE 0.000001
#define fracDiff(a,b) (fabs(a-b)/b)
using namespace std;
typedef int idType;
namespace EvolvingClocks {
enum interfaceType { empty = 0, clockBase, handEnd, gearEdge, gearTop, gearBottom, INTERFACE_MAX };
enum clockDesign { plate = 0, basicPendulum, brokenPendulum, doublePendulum, ratchetPendulum, ratchetPendulumThreeGears, ratchetPendulumBroken, ratchetPendulumWithHand };
enum periodType { pendulum, plainGear, gearWithHand };
class PeriodInfo {
private:
float period_;
periodType type_;
idType id_; // Of component with this period
public:
PeriodInfo(float p, periodType t, idType id){ period_ = p; type_ = t; id_ = id; }
float period(){ return period_; }
periodType type(){return type_; }
idType componentId() { return id_; }
};
class Component;
class Connection {
private:
interfaceType myInterface_;
interfaceType otherInterface_;
Component* other_;
public:
Connection();
Connection(Component*,interfaceType, interfaceType);
bool isOK(bool verbose=false);
interfaceType myInterface() {return myInterface_;}
interfaceType otherInterface() {return otherInterface_;}
Component* otherComponent() {return other_;}
string description();
};
class Component {
friend class Clock;
protected:
idType id_;
deque<Connection> connections_;
public:
Component(){id_ = rand();}
Component(idType id){id_ = id;}
virtual ~Component(){} //otherwise dynamic casts don't work
bool isOK(bool verbose=false);
void link(Component*,interfaceType,interfaceType);
void linkOneWay(Component*,interfaceType,interfaceType);
void delink(Component*);
bool hasLinkToBase();
bool hasLinkToBase(deque<idType>);
bool hasLinkToBaseExcluding(idType);
virtual unsigned int maxConnectionsOfType(interfaceType iType){return 0;}
unsigned int nConnectionsOfType(interfaceType);
unsigned int nTargetsOfType(interfaceType);
unsigned int freeConnectionsOfType(interfaceType);
deque<interfaceType> freeConnectionTypes();
interfaceType randomFreeConnectionType();
bool hasFreeConnections();
string description(){ return ""; }
idType identifier(){return id_;}
void setIdentifier(idType id){id_=id;}
};
class Hand : public Component {
private:
float length_;
public:
unsigned int maxConnectionsOfType(interfaceType iType);
~Hand(){};
Hand(float l) { length_ = l; id_ = rand(); }
Hand(float l, idType id) { length_ = l; id_ = id; }
Hand();
float period() { return 2*3.14159*sqrt(length_/9.8); }
string description();
Hand(const Hand& h){ length_ = h.length_ ; id_ = h.id_; }
};
class Gear : public Component {
private:
// float radius_;
int teeth_;
public:
~Gear(){};
bool isOK(bool verbose=false);
bool hasLoop();
unsigned int maxConnectionsOfType(interfaceType iType);
// Gear(float r, int t) { radius_ = r; teeth_ = t; }
Gear(int t,idType id) { teeth_ = t; id_ = id; }
Gear(int t) { teeth_ = t; id_ = rand(); }
Gear();
int nTeeth() { return teeth_; }
string description();
Gear(const Gear& g){ teeth_ = g.teeth_ ; id_ = g.id_; }
};
class Backplate : public Component {
public:
unsigned int maxConnectionsOfType(interfaceType iType);
~Backplate(){};
Backplate(){};
string description(){ return "backplate"; }
};
class Clock {
private:
deque<Hand> hands_;
Backplate backplate_;
deque<Gear> gears_;
idType nextId_;
public:
Clock(){ resetIdentifiers(); };
Clock(clockDesign);
Clock(int);
bool isOK(bool verbose=false);
deque<PeriodInfo> periods(bool verbose=false);
void resetIdentifiers();
int nPieces(){return (1+gears_.size()+hands_.size());}
deque<Component*> freeComponents();
Component* randomFreeComponent();
void display ();
void addRandom();
Clock(Clock*);
};
}
#endif