greenhouse
a creative coding toolkit for spatial interfaces

Sketch

Convenient methods for drawing shapes, lines, points, etc. via OpenGL.

  • Sketch ()
  • SetStroked ()
  • QueryStroked ()
  • SetFilled ()
  • QueryFilled ()
  • SetFillColor ()
  • FillColor ()
  • SetStrokeColor ()
  • StrokeColor ()
  • SetStrokeWidth ()
  • StrokeWidth ()
  • Clear ()
  • DrawPoint ()
  • DrawLine ()
  • DrawBezier ()
  • DrawArc ()
  • DrawEllipse ()
  • DrawRect ()
  • DrawRoundedRect ()
  • DrawTriangle ()
  • DrawShape ()
  • Append ()
  • Cut ()

Example

#include "Greenhouse.h"

struct DotMatrix  :  public Sketch
{ float64 width;

  DotMatrix (float64 w)
    { width = w;
      SetStrokeWidth(.5);
      for (int i = 0  ;  i <= 10  ;  ++i)
        { for (int j = 0; j <= 10; ++j)
            { SetStroked (false);
              if (Random (0, 5) < 1)
                SetFillColor (Color (0.8, 0.4, 0, 0.6));
              else if (FillColor() != Color (0.61, 0.78, 0.75, 0.5))
                SetFillColor (Color (0.61, 0.78, 0.75, 0.5));
              DrawEllipse (Vect (i * width/10.0, -j * width/10.0, 1),
                           (i + j + 2) * width/300.0,
                           (i + j + 2) * width/300.0);
                           //Vect ((i + j + 2) * width/500.0, 0, 0),
                           //Vect (0, (i + j + 2) * width/500.0, 0));
              SetStrokeColor (Color (0.91, 0.91, 0.91, 0.6));
              SetStrokeWidth (1.5);
              DrawPoint (Vect (i*width/10, j*-width/10, 0));
            }
        }
    }
};

struct LinesCreep  :  public Sketch
{ Trove <float64> widths;
  Trove <float64> positions;
  Trove <float64> speeds;
  int64 num;
  float64 width;

  LinesCreep (float64 w)
    { width = w;
      num = 30;
      for (int i = 0  ;  i < num  ;  ++i)
        { widths . Append (Random(1, 4));
          positions . Append (Random(0, width));
          speeds . Append (Random(0, .25));
        }
    }

  void DrawSelf ()
    { Clear();
      SetStrokeColor (Color (0.61, 0.78, 0.75, 0.7));
      for(int i = 0  ;  i < num  ;  i++)
        { positions . Nth(i) += speeds . Nth(i);
          if(positions . Nth(i) > width)
            positions . Nth(i) = 0;
          SetStrokeWidth (widths . Nth (i));
          DrawLine (Vect (positions . Nth(i), 0, 0),
                    Vect (positions . Nth (i), width, 0));
        }
    }
};

struct InscribedCurve  :  public Sketch
{ float64 width;

  InscribedCurve (float64 w)
    { width = w;
      SetFillColor (Color (0.61, 0.78, 0.75, 0.5));
      SetFilled(true);
      DrawRect (Vect (0, 0, 0), width, width);
      DrawRect (Vect (-width/2, 0, 0), width, width, Vect (-.707, 0, .707), Vect (0, 1, 0));
      SetStrokeColor (Color (0.8, 0.4, 0,0.7));
      SetStrokeWidth (1);
      DrawBezier (Vect (width/2, width/2, 0),
                  Vect (-width/2, -width/2, 0),
                  Vect (-width/2, width/2, 0),
                  Vect (width/2, -width/2, 0));
    }
};

struct ArcNest  :  public Sketch
{ float64 width;

  ArcNest (float64 w)
  { width = w;
    INFORM ("Default drawing settings are:\nStroked: " + ToStr (QueryStroked ()) +
           "\nStrokeColor: " + ToStr (StrokeColor ()) +
           "\nStrokeWidth: " + ToStr (StrokeWidth ()) +
           "\nFilled: " + ToStr (QueryFilled ()) +
           "\nFillColor: " + ToStr (FillColor ()));
    SetStrokeWidth (5);
    for (int i = 0  ;  i <= 20  ;  i+=2)
      { Vect ax = Norm () . RotateAbout (ZeroVect,
                                         - Over (),
                                         i/100.0) . Norm ();
        //  axis of rotation pitches more and more
        DrawArc (Vect (0, 0, 1),
                 PI,
                 PI,
                 i * width/20.0,
                 ax,
                 Over ());
      }
  }
};

struct ParentalUnit  :  public Thing
{ ParentalUnit ()
  { }

  void Blurt (BlurtEvent *e)
    { if (Utters (e, "w"))
        IncRotation (WrangleRay (Feld () -> Over ()), -PI / 18);
      else if (Utters (e, "s"))
        IncRotation (WrangleRay (Feld () -> Over ()), PI / 18);
      else if (Utters (e, "a"))
        IncRotation (WrangleRay (Feld () -> Up ()), -PI / 18);
      else if (Utters (e, "d"))
        IncRotation (WrangleRay (Feld () -> Up ()), PI / 18);
    }
};

void Setup()
{ SetDepthSorting (false);

  float64 square_size = Feld() -> Height() * 0.3;
  if (Feld () -> Width () < Feld () -> Height ())
    square_size = Feld () -> Width () * 0.3;

  Sketch *t = new Sketch ();
  t -> IncTranslation (Vect (0, 0, -1));
  t -> SetStroked (false);
  t -> SetFillColor (Color (0.9, 0.3, 0, 0.25));
  t -> DrawTriangle (Vect (-square_size, square_size, 0),
                     Vect (-square_size, -square_size, 0),
                     Vect (square_size, -square_size, 0));

  t -> SetFillColor (Color (1, 1, 1, 0.035));
  t -> DrawRoundedRect (Vect (0, 0, 0),
                        Feld () -> Width () * 0.82,
                        Feld () -> Height () * 0.82,
                        Feld () -> Height () / 10.0);
  t -> SetFilled (false);
  t -> SetStrokeWidth (0.2);
  t -> DrawRoundedRect (Vect (0, 0, 0),
                        Feld () -> Width () * 0.83,
                        Feld () -> Height () * 0.83,
                        Feld () -> Height () / 10.0);

  ParentalUnit *p = new ParentalUnit ();
  p -> AppendKid (new LinesCreep (square_size));
  p -> AppendKid (new InscribedCurve (square_size));
  p -> AppendKid (new DotMatrix (square_size));
  p -> AppendKid (new ArcNest (square_size));
  p -> AppendKid (t);
  p -> SlapOnFeld ();

  Find <InscribedCurve> () -> IncTranslation (Vect (-square_size/2, square_size/2,0));
}