Difficulty:: Easy.
Next Tutorial: The Recursive Octahedron

The Senoid Wave

This example is similar to the Ring of Spheres. We use again the for loop to generate a complex 3D figure. The main difference is that in this case we will create two senoid figures. Each will consist of only one solid object. One will be build from cylinders and the other from cubes.

We get one solid object by adding each individual object we generate. Let's see the process step by step.

We create the senoid object creating an aggregate of cylinders using a for loop:

We begin creating the CompositeComponent aggregation. The CompositeComponent is only used when we want to create Unions, Differences or Intersections by aggregation. Which it is usually the case in loops. The CompositeComponent Object inherits from Component, so then, the created object can be used as any other Component.

CompositeComponent wave = Union::create();

And then we add objects to the wave by simply using the insertion («) C++ operator.

   for (int i=0; i<180; i++){
	double ang = i*M_PI/180;
	double height = 20*sin(ang);
	Component cylinder = Cylinder( 0.2 , height );
	wave << cylinder.translatedCopy( float(i)/5 , 0 , height/2 );
   }

As you can see the integer i is used as angle, and with it the height of the senoid is computed. A cylinder with the computed height is created in each iteration, and it is added to the sin_cyl object, which will contain the (OpenScad) addition of all the cylinders. Each cylinder is translated to generate the wave object.

Fynally the OpenScad code is generated:

  IndentWriter writer;
  ofstream os("sin.scad");
  writer << wave;
  os << writer;
  os.close();

Here is the full code for this example

#include <components/Cylinder.h>
#include <core/IndentWriter.h>
#include <core/Union.h>
 
#include <iostream>
#include <fstream>
#include <core/Math.h>
 
using namespace std;
 
int main(int argc, char **argv)
{
   CompositeComponent wave = Union::create();
   for (int i=0; i<180; i++){
	double ang = i*M_PI/180;
	double height = 20*sin(ang);
	Component cylinder = Cylinder( 0.2 , height );
	wave << cylinder.translatedCopy( float(i)/5 , 0 , height/2 );
   }
   IndentWriter writer;
   writer << wave;
   std::cout << writer;
}

Next Tutorial: The Recursive Octahedron

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