Course Home | Documentation | Lab Hours/Tutoring | Projects | Quizzes | Schedule | Submit

Saint Louis University

Computer Science 1050
Introduction to Computer Science: Multimedia

Michael Goldwasser

Spring 2016

Dept. of Math & Computer Science

Lecture Notes: Drawing Shapes


Note: This document is intended only to be a very brief summary of basic drawing commands. 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.


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).


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.


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. Processing includes built-in definitions for constants PI, TWO_PI, HALF_PI, QUARTER_PI. Note well that by their clockwise convention, the angle HALF_PI is downward. (Contrary to the typical mathematical conventions for angles, which measure counterclockwise.)

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:


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.


stroke 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.


Michael Goldwasser
CSCI 1050, Spring 2016
Last modified: Monday, 18 January 2016
Course Home | Documentation | Lab Hours/Tutoring | Projects | Quizzes | Schedule | Submit