Difficulty:: Easy.
Next Tutorial: The rotated extruded torus
Polygon2D is a new class which allows to define a set of points at x-y plane. Each point is forth added in order, by the addPoint function, up to reach the last. Thus, if we want to build a pentagon, we would do:
Polygon<Point2D> base; base.addPoint(Point2D(0, 2)); base.addPoint(Point2D(2, 4)); base.addPoint(Point2D(4, 2)); base.addPoint(Point2D(3, 0)); base.addPoint(Point2D(1, 0));
Declares a 2D object called base, and sets the points by making up vertices of the pentagon at coordinates x-y.
Now, we can extrude this polygon with the high we want through z axis. For this, we need to create a new component from PolygonalPrism class:
Component prism(PolygonalPrism(base, 1.0));
This code extrudes the polygon with a width of 1 mm.
If we want to generate the OpenSCAD code for this cylinder, we simply run:
IndentWriter writer; writer << prism; std::cout << writer;
This will send to the standard output the following:
polyhedron(points=[[0.000, 2.000, 0.000], [0.000, 2.000, 1.000], [2.000, 4.000, 0.000], [2.000, 4.000, 1.000], [4.000, 2.000, 0.000], [4.000, 2.000, 1.000], [3.000, 0.000, 0.000], [3.000, 0.000, 1.000], [1.000, 0.000, 0.000], [1.000, 0.000, 1.000]], triangles=[[1, 3, 5], [8, 6, 0], [1, 5, 7], [6, 4, 0], [1, 7, 9], [4, 2, 0], [0, 3, 1], [0, 2, 3], [2, 5, 3], [2, 4, 5], [4, 7, 5], [4, 6, 7], [6, 9, 7], [6, 8, 9], [8, 1, 9], [8, 0, 1]]);
The full code is:
int main() { // Declare a 2D object Polygon<Point2D> base; // Add five vertices in order. //Fist would be at x=0, y=2. base.addPoint(Point2D(0, 2)); //Second would be at x=2, y=4. base.addPoint(Point2D(2, 4)); //Third would be at x=4, y=2... base.addPoint(Point2D(4, 2)); base.addPoint(Point2D(3, 0)); base.addPoint(Point2D(1, 0)); // Create a prism with five sides and 1 mm of width. Component prism(PolygonalPrism(base, 1.0)); //Generate the OpenScad code IndentWriter writer; writer << prism; cout << writer; return 0; }
Next Tutorial: The rotated extruded torus