Difficulty:: Medium.
Next Tutorial: Creating a Toroid Primitive

The Random Forest (adding colors)

#include <components/Cube.h>
#include <components/Cylinder.h>
#include <core/IndentWriter.h>
#include <core/Difference.h>
#include <core/Union.h>
#include <core/Intersection.h>
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#include <iostream>
#include <fstream>
 
using namespace std;
 
//-- A simple pine tree... or a kind of.. :-)
Component gen_pine(double trunk_height, double trunk_radius)
{
    Component trunk = Cylinder(trunk_radius*2, trunk_height).color(0.5,0.2,0);
    Component top = Cylinder(trunk_radius*10 , 0 , trunk_height).color(0,1,0);
    Component pine = trunk.translate(0,0,trunk_height/2) + top.translate(0,0,trunk_height);
 
    return pine;
}
 
int main(int argc, char **argv)
{
	 IndentWriter writer;
 
         // Open a file where it's going to generate the OpenSCAD code.
         ofstream os("forest.scad");
 
         srand(time(0));
 
         Component ground(Cube(100,100,1));
 
         //-- Create a forest
         Component scenario = ground.translate(50,50,-0.5).color(0.5,0.5,0.1);
 
         for (int i=0; i<20; i++)
         {
             double radius = float(rand())/RAND_MAX+0.5;
             //cerr << "radius="<<radius << endl;
             double height = 20.0*rand()/RAND_MAX+10;
             //cerr << "height="<<radius << endl;
 
             Component pine = gen_pine(height, radius);
 
             double x = 100.*rand()/RAND_MAX;
             double y = 100.*rand()/RAND_MAX;
 
             pine.translate(x,y,0);
 
             scenario = scenario + pine;
         }
 
         writer << scenario;
 
	 os << writer;
	 os.close();
 
         return 0;
}

Next Tutorial: The Toroid Primitive

Recent changes RSS feed Creative Commons License Donate Minima Template by Wikidesign Driven by DokuWiki