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
|
#pragma once
#include <iostream>
#include "Shape.hh"
#include <cmath>
#include<fstream>
using namespace std;
class Cube : public Shape {
private:
double s;
void facet(ostream &s, double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3,
double z3) {
double Ax = x2 - x1;
double Ay = y2 - y1;
double Az = z2 - z1;
double Bx = x3 - x1;
double By = y3 - y1;
double Bz = z3 - z1;
double nx = Ay * Bz - Az * By;
double ny = Az * Bx - Ax * Bz;
double nz = Ax * Bz - Ay * Bx;
s << "facet normal " << nx << " " << ny << " " << nz << "\n";
s << "outer loop\n";
s << "vertex" << x1 << " " << y1 << " " << z1 << "\n";
s << "vertex" << x2 << " " << y2 << " " << z2 << "\n";
s << "vertex" << x3 << " " << y3 << " " << z3 << "\n";
s << "endloop\n";
s << "endfacet\n";
}
void
rectangleFace(ostream &s, double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3,
double z3, double x4, double y4, double z4) {
facet(s, x1, y1, z1, x2, y2, z2, x3, y3, z3);
facet(s, x1,y1,z1 , x3,y3,z3 , x4,y4,z4); // it may be x1,y1,z1 , x3,y3,z3 , x4,y4,z4
}
public:
Cube(double x_, double y_, double z_ , double s_) : Shape(x_, y_ , z_) , s(s_) {}
void print (const char filename[]) {
ofstream f(filename);
f << "solid Cube \n" ;
rectangleFace(f,x,y,z, x+s,y,z , x+s,y+s,z, x,y+s,z);
rectangleFace(f,x,y,z , x,y+s,z+s , x,y+s,z, x,y,z+s);
rectangleFace(f,x,y+s,z , x+s,y+s,z+s , x+s,y+s,z , x,y+s,z+s );
rectangleFace(f,x+s,y,z , x+s,y+s,z , x+s,y+s,z+s , x+s,y,z+s );
rectangleFace(f,x,y,z , x+s,y,z , x+s,y,z+s , x,y,z+s );
rectangleFace(f,x,y,z+s , x+s,y,z+s , x+s,y+s,z+s , x,y+s,z+s );
f<<"endsolid Cube\n";
};
|