Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 144
Introduction to Computer Science: Multimedia

Michael Goldwasser

Spring 2015

Dept. of Math & Computer Science

Lecture Notes: Drawing


Note: This document is intended only to be a very brief outline of the content we covered. Far more thorough discussion and documentation can be found in the Processing reference guide, various tutorials, cheat sheets, and our text book


size

The size function can be used to change the size of our drawing canvas. The typical syntax we will use is

  size(w,h);
where w and h define the width and height, respectively.

By default, the canvas is 100x100.


background

The background function can be used to change the appearance of the canvas background. By default the background is a light gray color. Colors for the background (and most other shapes and lines) can be controlled using a variety of models:


More about colors

To assist in color selection, see menu item Tools → Color Selector.

Note as well that Processing technially allows two different modes for designating a color. The RGB mode is the default. There is a separate mode known as HSB (hue-saturation-brightness). The colorMode function can be used to change the mode, if desired.

There are also a few other ways to define colors (e.g., using hexadecimal notation, which is common in computer science), but we'll ignore those for now.


Shapes

Processing supports many primitive shapes (and more advanced techniques to define other shapes). We begin by using a line and a rectangle as representative shapes. We then discuss ways in which various attributes of these shapes can be controlled. Then we provide a summary of several other primitive shapes.

line

The syntax for drawing a line has the form:

  line(x1, y1, x2, y2);
where x1 and y1 define the coordinates of one endpoint, and x2 and y2 define the other endpoint.

rect

A rectangle can be drawn with the rect function. By default, it is described using the following syntax:

  rect(tlx, tly, w, h);
where parameters tlx and tly define the top-left x and y coordinates, and w and h define the width and height, respectively. There is a rectMode function that can be used to define other modes for describing rectangles in a program (e.g., describing it relative to its center rather than top-left, or by specifying two opposite corners). Most important is that you understand which mode is currently in use.

By default, the corners of the rectangle are "squared off". But you can define rounded rectangles by specifying an optional fifth parameter that is the radius of the circle used to define a rounded corner, as in

  rect(20, 40, 50, 30, 5);    // corners rounded with radius 5
If you want even more fine-tune control, you can define the radius for each of the four corners separately (see documentation for the order of these parameters).

stroke color, weight, cap, join

The stroke that is used for drawing a line, and for drawing the border of a shape such as a rectangle, can be customized in several ways.

fill color for shapes

For shapes such as rectangles that have an interior, you may control how that interior is rendered. You may switch back and forth between settings as your script progresses.

  noFill();
is used to say that subsequent shapes that are drawn be unfilled. This is the initial default.

  fill(color);
is used to indicate that subsequent shapes are drawn filled with the given color. The color can either be a single value, for grayscale, or an RGB triple, or any other valid color specification.

layering of shapes and transparency

By default, if the geometry of two shapes overlaps, whatever is drawn later will obscure the first. But it is possible to define colors with an additional optional transparency, known as its "alpha" value, that defines a level of opacity ranging from 0=transparent to 255=opqaue. This allows for various forms of blending of a new color with whatever it obscures.

point

The command
  point(x, y);
draws a point at the given coordinate, using the current stroke color and weight.

triangle

An arbitrary triangle can be rendered using the command

  triangle(x1, y1, x2, y2, x3, y3);

quad

An arbitrary quadrilateral can be rendered using the command

  quad(x1, y1, x2, y2, x3, y3, x4, y4);

polygons

There is a way to draw polygons with more than four vertices, but it is not quite as straightforward. We'll revisit this another time, but for those who wish to read ahead, see the documentation for the beginShape() command.

ellipse

An ellipse (and thus a circle, as a special case), can be drawn as

  ellipse(cx, cy, w, h);
which defines an ellipse. By default, the meaning is that the ellipse is centered at (cx,cy) and has width w and height h. However, an ellipseMode method can be used to select a different convention for indicating the geometry of the ellipse.

arc

An arc, which is a portion of an ellipse shape, can be drawn using a syntax such as:

  arc(cx, cy, w, h, start, stop);
The first four parameters define the underlying ellipse shape, as with the ellipse command. The next two parameters define a start angle and stop angle for the arc, with angles measured in radians, going clockwise from 0=right returning to 2π, which is also to the right. Note that Processing includes built-in definitions for constants PI, TWO_PI, HALF_PI, QUARTER_PI.

There are several additional options that further define precisely how the boundary of the arc is rendered, and if filled, what portion is filled. It would be best to refer back to the formal documentation, which includes nice figures, but in short:


Michael Goldwasser
CSCI 144, Spring 2015
Last modified: Wednesday, 14 January 2015
Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit