Brandon
2012-01-23 23:25:54
I decided to write the entire SRL include into a language I know quite well.. C++.. NVM with a little fiddling, I got around it.. Got it forwarded nicely.. Only thing left is the TPA's.. I can't seem to find the source code for TPA's.. like making a TPA in c++ or something that I can translate at least. Problem is how to dynamically allocate structs aka my TPA's..
Non-Template version (UPDATED this version only.. Now prints points and TPA's but only the first point in the TPA Why?):
Non-Template version (UPDATED this version only.. Now prints points and TPA's but only the first point in the TPA Why?):
#ifndef TYPES_H_INCLUDED
#define TYPES_H_INCLUDED
#include <math.h>
#include <vector>
#include <assert.h>
#include <sstream>
using namespace std;
/** Definitions **/
#define LEN(a) (sizeof(a)/sizeof(*a))
#define HIGH(a) (sizeof(a)/sizeof(*a) - 1)
#define Repeat do{
#define Until(condition) }while(!(condition));
#define writeln(Output) (cout<<Output<<"\n");
/** Types **/
struct Box;
struct Point
{
int X, Y;
Point() {X = Y = 0;}; //Default constructor.
Point(int X_, int Y_) : X(X_), Y(Y_) {}; //Alternate constructor.
~Point(){}; //Destructor.
int operator == (const Point &PT) const //Are the points Equal?
{
return ((X == PT.X) && (Y == PT.Y));
}
int operator != (const Point &PT) const //Are the points Not equal?
{
return ((X != PT.X) && (Y != PT.Y));
}
Point& operator = (const Point& PT)
{
if (this != &PT)
{
X = PT.X;
Y = PT.Y;
}
return *this;
}
Point operator += (Point PT)
{
X += PT.X;
Y += PT.Y;
return *this;
}
Point operator -= (Point PT)
{
X -= PT.X;
Y -= PT.Y;
return *this;
}
Point MidPointBox (const Box &B);
friend ostream& operator << (ostream& Str, const Point &PT) //For use with Writeln & cout.
{
Str<<"("<<PT.X<<", "<<PT.Y<<")";
return Str;
}
friend inline Point operator + (Point P1, Point P2) //Add two points.
{
return Point(P1.X + P2.X, P1.Y + P2.Y);
}
friend inline Point operator - (Point P1, Point P2) //Subtract two points.
{
return Point(P1.X - P2.X, P1.Y - P2.Y); //No absolute value needed since points can have negative values.
}
friend inline int Distance(Point P1, Point P2) //Friend is for Inheritance and friendship of a Class/Struct.
{
return (hypot(P1.X - P2.X, P1.Y - P2.Y));
}
friend inline Point Invert(Point PT) //Invert a point.
{
return Point(-PT.X, -PT.Y);
}
};
struct Box
{
int X1, Y1, X2, Y2;
Box(){X1 = Y1 = X2 = Y2 = 0;}; //Default Constructor.
Box(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_) {}; //Alternate Constructor.
~Box(){}; //Destructor.
friend int operator == (const Box &B, const Box &B2) //Are the Boxes the same?
{
return ((B.X1 == B2.X1) && (B.X2 == B2.X2) && (B.Y1 == B2.Y1) && (B.Y2 == B2.Y2));
}
friend inline int operator != (const Box &B, const Box &B2) //Are the Boxes Not the same?
{
return !(B == B2);
}
Box operator += (Box B)
{
X1 += B.X1;
Y1 += B.Y1;
X2 += B.X2;
Y2 += B.Y2;
return *this;
}
Box operator -= (Box B)
{
X1 -= B.X1;
Y1 -= B.Y1;
X2 -= B.X2;
Y2 -= B.Y2;
return *this;
}
Box PointToBox(Point UpperLeft, Point LowerRight)
{
return Box(UpperLeft.X, UpperLeft.Y, LowerRight.X, LowerRight.Y);
}
Box& operator = (const Box& B)
{
if (this != &B)
{
X1 = B.X1;
Y1 = B.Y1;
X2 = B.X2;
Y2 = B.Y2;
}
return *this;
}
friend ostream& operator << (ostream& Str, const Box &B) //For use with Writeln & cout.
{
Str<<"("<<B.X1<<", "<<B.Y1<<", "<<B.X2<<", "<<B.Y2<<")";
return Str;
}
friend inline Box operator + (Box B1, int S) //Increase Box Size.
{
return Box(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);
}
friend inline Box operator - (Box B1, int S) //Decrease Box Size.
{
if (((B1.X1 - S) < 0) || ((B1.Y1 - S) < 0) || ((B1.X2 - S) < 0) || ((B1.Y2 - S) < 0))
return Box(-1, -1, -1, -1); //Return -1's if its not a valid box.
else if (((B1.X1 - S) == 0) || ((B1.Y1 - S) == 0) || ((B1.X2 - S) == 0) || ((B1.Y2 - S) == 0))
return Box(0, 0, 0, 0); //Returns 0's because it becomes a point.
else
return Box(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S); //Return the resized box.
}
};
struct PointArray
{
private:
Point* P;
int rows;
public:
Point& operator ()(int I)
{
return P[I]; //Return A Point.
}
Point& operator[](int I)
{
assert(P != 0 && I >= 0 && I < rows); //P <> 0 & I Must be >=0 & I < num of rows. Asserts to terminate the program upon Bad Input.
return P[I]; //Return A Point.
}
const Point& operator[](int I) const
{
assert(P != 0 && I >= 0 && I < rows); //P <> 0 & I Must be >=0 & I < num of rows.
return P[I]; //Return A Point.
}
int Rows() const
{
return rows;
}
operator const Point* () const
{
return P;
}
operator Point* ()
{
return P;
}
Point* Resize(int NewSize);
PointArray& operator = (const Point& PT)
{
if (this[LEN(this)] != &PT)
{
P[LEN(this)] = PT;
}
return *this;
}
friend ostream& operator << (ostream& Str, const PointArray &PA) //For use with Writeln & cout.
{
Str<<"[";
for (int I = 0; I < LEN(PA); I++)
{
Str<<PA[I];
}
Str<<"]";
return Str;
}
PointArray(const PointArray &N);
PointArray(int I = 2):
P(I > 0 ? new Point[I] : 0), rows(I) { //Created a New Point.
}
~PointArray()
{
delete[] P; //The new operator was used.. Must delete all instances of P.
P = 0;
rows = 0;
}
};
Point Point::MidPointBox(const Box& B)
{
return Point(((B.X1 + B.X2)/2), ((B.Y1 + B.Y2)/2));
};
#endif //TYPES_H_INCLUDED
Template Version (Does not need forwarding):
#ifndef TYPEDEFS_H_INCLUDED
#define TYPEDEFS_H_INCLUDED
#include <math.h>
template <class T> struct Point;
template <class T> struct Box;
template <class T>
struct Point
{
T X, Y;
Point() {X = Y = 0;}; //Default constructor.
Point(int X_, int Y_) : X(X_), Y(Y_) {}; //Alternate constructor.
~Point(){};
int operator == (const Point &PT);
int operator != (const Point &PT){return ((X != PT.X) && (Y != PT.Y));};
Point& operator = (const Point& PT){if (this != &PT){X = PT.X; Y = PT.Y;} return *this;};
Point operator += (Point PT){X += PT.X; Y += PT.Y; return *this;};
Point operator -= (Point PT){X -= PT.X; Y -= PT.Y; return *this;};
Point MidPointBox (Box<T> B){return Point<T>(((B.X1 + B.Y1)/2), ((B.X2 + B.Y2)/2));};
friend inline Point<T> operator + (Point<T> P1, Point<T> P2){return Point<T>(P1.X + P2.X, P1.Y + P2.Y);};
friend inline Point<T> operator - (Point<T> P1, Point<T> P2){return Point<T>(P1.X - P2.X, P1.Y - P2.Y);};
friend inline T Distance(Point<T> P1, Point<T> P2){return T(hypot(P1.X - P2.X, P1.Y - P2.Y));};
friend inline Point<T> Invert(Point<T> PT){return Point<T>(-PT.X, -PT.Y);};
};
template <class T>
struct Box
{
int X1, Y1, X2, Y2;
Box(){X1 = Y1 = X2 = Y2 = 0;}; //Default Constructor.
Box(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_) {}; //Alternate Constructor.
~Box(){}; //Destructor.
friend int operator == (const Box<T> &B, const Box<T> &B2){return ((B.X1 == B2.X1) && (B.X2 == B2.X2) && (B.Y1 == B2.Y1) && (B.Y2 == B2.Y2));}
friend inline int operator != (const Box<T> &B, const Box<T> &B2){return !(B == B2);}
Box operator += (Box B){X1 += B.X1; Y1 += B.Y1; X2 += B.X2; Y2 += B.Y2; return *this;}
Box operator -= (Box B){X1 -= B.X1; Y1 -= B.Y1; X2 -= B.X2; Y2 -= B.Y2; return *this;}
Box PointToBox(Point<T> UpperLeft, Point<T> LowerRight){return Box<T>(UpperLeft.X, UpperLeft.Y, LowerRight.X, LowerRight.Y);}
Box& operator = (const Box& B){if (this != &B){X1 = B.X1; Y1 = B.Y1; X2 = B.X2; Y2 = B.Y2;} return *this;}
friend inline Box<T> operator + (Box<T> B1, int S){return Box<T>(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);}
friend inline Box<T> operator - (Box<T> B1, int S)
{
if (((B1.X1 - S) < 0) || ((B1.Y1 - S) < 0) || ((B1.X2 - S) < 0) || ((B1.Y2 - S) < 0))
return Box<T>(-1, -1, -1, -1);
else if (((B1.X1 - S) == 0) || ((B1.Y1 - S) == 0) || ((B1.X2 - S) == 0) || ((B1.Y2 - S) == 0))
return Box<T>(0, 0, 0, 0);
else
return Box<T>(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);
}
};
#endif // TYPEDEFS_H_INCLUDED