CAGD course

Programming languages

We encourage math and computer science students to explore different ways to generate graphics. Matlab and Java are both platforms that require only a minimum amount of learning, and provide efficient graphics tools. Other programming languages and development tools are welcome.

 

The first things you’ll need to know are

1.     How to write a function with input and output parameters.

2.     How to draw a line segment between two 2d points.

Here is how you can do it in Matlab and in Java:

Matlab

Matlab is the most recommended tool to use for submitting assingments in this course. Matlab is a powerful tool for matrix and vector computations. It has the basic programming elements of a procedural language (functions, variables, loops) and even a bit of object-oriented capabilities (classes). It has powerful graphics tools, for displaying 2d and 3d graphics.

The basic mode of work is from command-line. You assign values to variables, and call functions from command line. Any variable in matlab is a matrix. A scalar value is a 1*1 matrix. A vector is a 1*k or k*1 matrix. example:

>x=1:10
x=[ 1 2 3 4 5 6 7 8 9 10 ]
>y=x*2
y=[ 1 2 3 4 5 6 7 8 9 10 ]
>plot(x,y)

The function plot(x,y), plot a polygon between (1,2),(2,4,),...,(10,20). Use plot and plot3d to draw polygons. Any time you need help on a command, use the “help” command. example:

>help plot
...

A Matlab function is written in an M-file (a file with extension *.m). Write it using any text editor. Make sure that the name of the function is the same as the file name. example:

The file drawarc.m contain the drawarc function

function [x,y]=drawarc (n)
t=0:n;
ratio=2*(t/n - 0.5);
x=sin(ratio*pi);
y=cos(ratio*pi);
plot(x,y);
 

The function is called with parameter n that represents here the number of samples to draw along the arc. It computes parameters between -1.0..1.0 with n intervals, calculates points x,y on a closed circle (radius=1), and draws the circle as a polygon and return the x,y coordinates as output vectors.
 

JAVA

Java is a powerful object-oriented programming language which is famous for its portability – it can execute on any platform. Java can be used to create full applications, but for the purposes of our course, we recommend the use of applets. An applet is a small application embedded into an html page, and they can run on any internet browser. Applets have easy-to-use 2d graphics commands, and they can be easily embedded in an html page. Java is easy to learn if you are familiar to C++.

 

In order to develop an applet you’ll need to:

  1. Write a Java source file that defines the class behind the applet (*.java).
  2. Compile the source file to generate the compiled class file(s) (*.class).
  3. Write an html file that invokes the applet.

 

The applet runs by loading the html file in an internet browser. You’ll need to have a Java compiler. It is downloadable from http://www.java.sun.com (Follow "Products & APIs", then look for "Java 2 SDK, Standard Edition, v 1.3") . Another option is to use Microsoft J++. Many Java examples can be found on the internet – use them to learn more.

Here is an example of a simple applet that draws a circle.

File: CAGDApplet.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class CAGDApplet extends Applet {
  public void paint (Graphics g)
  {
    DrawArc(g);
  }

public void DrawArc (Graphics g)
  {
    //g.drawLine(0,0,300,300);
    double pi=3.14;
    int xPrev=150;
    int yPrev=50;
    for (double ratio=-1.0;ratio <= 1.0;ratio += 0.01)
    {
      int x=150 + (int)(100.0 * Math.sin (ratio * pi));
      int y=150 + (int)(100.0 * Math.cos (ratio * pi));
      g.drawLine(xPrev,yPrev,x,y);
      xPrev=x;
      yPrev=y;
    }
  }
}

Here is an html page that invokes this applet:

File: CAGD.html

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>
HTML Test Page
</TITLE>
</HEAD>
<BODY>

<APPLET
  CODEBASE = "."
  CODE = "CAGDApplet.class"
  NAME = "CAGDApplet"
  WIDTH= 300
  HEIGHT= 300
  HSPACE= 0
  VSPACE= 0
  ALIGN= top
>
</APPLET>
</BODY>
</HTML>

How to compile:

java CAGDApplet.java

The result is a java class named

CAGDApplet.class

Running:

open CAGD.html on your browser

The html file points to CAGDApplet.class, this is the complied CAGDApplet.java file, you need to have both files on the same directory. the circle below is the result, it is a java applet.


 
 
 

How to submit

There are two options for you to choose form:

 

Option #1: Submission on paper. You’ll need to submit

  1. A printout of the source files.
  2. A printout of graphical results.

 

Option #2: Electronic submission. Just write a web page where we can download your source files, and view the graphical results.