# Lecture 6 Data Structures & Drawing. Objective The objective is that you will understand: How to program the generation of 2D and 3D images. How to manipulate.

## Presentation on theme: "Lecture 6 Data Structures & Drawing. Objective The objective is that you will understand: How to program the generation of 2D and 3D images. How to manipulate."— Presentation transcript:

Lecture 6 Data Structures & Drawing

Objective The objective is that you will understand: How to program the generation of 2D and 3D images. How to manipulate those images through scaling, translation, rotation and projection. How to color and shade the images. Remove hidden surfaces and create realistic lighting effects. Before any of that however, we must learn more about how to describe a drawing in terms that a computer can understand.

The Basic 2d data structure – Point 2d Class A point is basically two coordinates, x and y, which are “real” numbers. This suggests that we might define a Java class to represent a point. The main task that objects of the Point2d class must accomplish is to store the x and y coordinates of the point.

The Basic 2d data structure – Point 2d Class

The Basic 2d data structure – Line2d Class Lines are the basic item of drawings. Each line has two end points. This suggests that an appropriate way to describe the drawings would be in terms of two classes: Line2d and Point2d. Thus to represent L1 in this scheme, we need 3 objects:

The Basic 2d data structure – Line2d Class The square has the corners, we are going to refer to the corners as nodes and they have special significance for the data structure. Notice in the drawing that a line is defined between two nodes (for example line L1 connects nodes N1 and N2) but not all nodes are connected by lines (for example N1 is not connected to N3).

The Basic 2d data structure – Line2d Class The square has the corners, we are going to refer to the corners as nodes and they have special significance for the data structure. Notice in the drawing that a line is defined between two nodes (for example line L1 connects nodes N1 and N2) but not all nodes are connected by lines (for example N1 is not connected to N3).

The Basic 2d data structure – Line2d Class

How come there are 4 nodes on this square, but you have 8 Point2d objects - surely your duplicating data unnecessarily. Well, yes and no. You can do it using 4 points and save your self 4 objects, but that means that those lines share instances of the points and CAN NEVER BE CHANGED INDEPENDANTLY. From a data modeling point of view, the corners of the square are in fact two points that JUST HAPPEN to be in the same place.

The Basic 2d data structure – Line2d Class

The Basic 2d data structure – Shape2d Class Any shape can be described as a collection of lines. Shape2d, is a class which maintains a list of all of the lines that make up a shape. This could be implemented in many ways, in fact we shall use a Vector which we will call lines. it is useful to know how many lines make up our shape. We use and integer named numberOfLines to keep count.

The Basic 2d data structure – Shape2d

The Basic 2d data structure – Drawing2d Class A drawing can consist of many shapes - We can therefore add a final class to our data model: Drawing2d, which can maintain another list (Vector) of the various shapes.

The Basic 2d data structure – Drawing2d Class

The Basic 2d data structure – Summary So, in full, we have a drawing object, which has a list of shape objects, which have lists of line objects, which have two point objects each.

The Basic 2d data structure – Class Diagram Drawing2d has Many Shapes Shape2d has many Lines Line2d has tow points

A working Gcanvas

The Completed system The final class diagram of the system should look something like this.

Exercises Describe the following data structures: Point Line Shape Drawing Describe the complete system and the class diagram for the following drawing.

Exercises

Download ppt "Lecture 6 Data Structures & Drawing. Objective The objective is that you will understand: How to program the generation of 2D and 3D images. How to manipulate."

Similar presentations