Commit f2a21194 authored by Hans Buchmann's avatar Hans Buchmann

svg

parent 65ec711c
......@@ -4,6 +4,63 @@
//-----------------------
#include <iostream>
#include <cmath>
#include <fstream>
//------------------------------------ class SVG
class SVG //Singleton
{
public:
static void line(double x0,double y0,
double x1,double y1);
static void dot(double x0,double y0,double r);
private:
static SVG svg; // the Singleton declared
static const char File[];
std::ofstream out;
SVG();
~SVG();
};
//------------------- implementation SVG(
const char SVG::File[]="geo2d.svg";
SVG SVG::svg; //constructor defined and initialized
SVG::SVG()
:out(File)
{
out<<
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"\n"
" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
"<svg xmlns=\"http://www.w3.org/2000/svg\"\n"
" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"
"<g stroke=\"black\">\n";
}
SVG::~SVG()
{
std::cout<<"figure in file '"<<File<<"'\n";
out<<"\n</g>\n"
"</svg>\n";
}
void SVG::line(double x0,double y0,double x1,double y1)
{
svg.out<<"<line x1=\""<<x0<<"\" "
"y1=\""<<y0<<"\" "
"x2=\""<<x1<<"\" "
"y2=\""<<y1<<"\"/>\n";
}
void SVG::dot(double x0,double y0,double r)
{
svg.out<<"<circle cx=\""<<x0<<"\" "
"cy=\""<<y0<<"\" "
"r=\""<<r<<"\"/>\n";
}
//------------------- implementation SVG)
//------------------------------------------------- Point
// declaration
......@@ -19,7 +76,10 @@ class Point
//| |------------- c++ reference
//|
//|------------------ only read for other
private:
friend class Line; // instances of Line acess the private parte of instances of Point
void svg(); //
double xc; //x-coordinate
double yc; //y-coordinate
};
......@@ -32,6 +92,7 @@ class Line
Line(const Point& p0,const Point& p1);
~Line();
private:
void svg();
Point p; //by value
Point q; //by value
};
......@@ -42,7 +103,7 @@ Point::Point(double x,double y)
:xc(x),yc(y) //init list
//xc=x;yc=y
{
std::cout<<"constructor Point "<<xc<<","<<yc<<"\n";
svg();
}
......@@ -73,13 +134,18 @@ double Point::distance(const Point& other) const
other.yc - yc);
}
void Point::svg()
{
SVG::dot(xc,yc,5);
}
//-------------------------------------------------- Line
// implementation
Line::Line(const Point& p0,const Point& p1)
:p(p0) //p by value copy constructor
,q(p1) //q by value copy constructor
{
std::cout<<"constructor Line\n";
svg();
}
Line::~Line()
......@@ -87,17 +153,18 @@ Line::~Line()
std::cout<<"destructor Line\n";
}
void Line::svg()
{
SVG::line(p.xc,p.yc,
q.xc,q.yc);
}
//--------------------------- use case
int main(int argc,char** args)
{
Point p(5, 7); //instance birth p constructor Point 5,7
Point q(8,11); //instance birth q constructor Point 8,11
Line l(p,q); //copy constructor Point 5,7
//copy constructor Point 8,11
//constructor Line
Point p(250, 350);
Point q(400,550);
Line l(p,q);
return 0;
} //death l destructor Line
//death l.q destructor Point 8,11
//death l.p destructor Point 5,7
//death q destructor Point 8,11
//death p destructor Point 5,7
}
......@@ -74,10 +74,8 @@ class Point
double distance(const Point& other) const;
//| |--- my own variables cannot be changed
//other cannot be changed
//getter
double x() const;
double y() const;
private:
friend class Line; //instances of line have access to my private part
void svg(); //draw the point
double xc; //x-coordinate
double yc; //y-coordinate
......@@ -140,9 +138,6 @@ double Point::distance(const Point& other) const //is a member of Point
yc- other.yc);
}
inline double Point::x()const{return xc;}
inline double Point::y()const{return yc;}
//----------------------------------------------- Line
// implementation
Line::Line(const Point& p,const Point& q)
......@@ -158,8 +153,8 @@ Line::~Line()
void Line::svg()
{
SVG::line(pp.x(),pp.y(), //x0,y0
qq.x(),qq.y()); //x1 y1
SVG::line(pp.xc,pp.yc, //x0,y0
qq.xc,qq.yc); //x1 y1
}
int main(int argc,char**args)
......
......@@ -12,8 +12,9 @@ class SVG //Singleton
static void line(double x0,double y0,
double x1,double y1);
static void dot(double x0,double y0,double r);
private:
static SVG svg; // the Singleton
static SVG svg; // the Singleton declared
static const char File[];
std::ofstream out;
SVG();
......@@ -22,7 +23,7 @@ class SVG //Singleton
//------------------- implementation SVG(
const char SVG::File[]="geo2d.svg";
SVG SVG::svg; //constructor
SVG SVG::svg; //constructor defined and initialized
SVG::SVG()
:out(File)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment