Top Banner
Lecture 6 Data Structures & Drawing
26

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.

Apr 01, 2015

Download

Documents

Nikolas Rayes
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Lecture 6Data Structures & Drawing

Page 2: 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.

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

Page 3: 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.

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.

Page 4: 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.

The Basic 2d data structure – Point 2d Class

Page 5: 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.

The Basic 2d data structure – Point 2d Class

Page 6: 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.

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:

Page 7: 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.

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

Page 8: 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.

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

Page 9: 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.

The Basic 2d data structure – Line2d Class

Page 10: 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.

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.

Page 11: 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.

The Basic 2d data structure – Line2d Class

Page 12: 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.

The Basic 2d data structure – Line2d Class

Page 13: 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.

The Basic 2d data structure – Line2d Class

Page 14: 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.

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.

Page 15: 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.

The Basic 2d data structure – Shape2d

Page 16: 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.

The Basic 2d data structure – Shape2d

Page 17: 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.

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.

Page 18: 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.

The Basic 2d data structure – Drawing2d Class

Page 19: 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.

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.

Page 20: 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.

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

Page 21: 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.

A working Gcanvas

Page 22: 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.

The Completed system• The final class diagram of the system should look

something like this.

Page 23: 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.
Page 24: 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.

Exercises• Describe the following data structures:

• Point• Line• Shape• Drawing• Describe the complete system and the class

diagram for the following drawing.

Page 25: 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.

Exercises

Page 26: 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.

Exercises