88
IT300: Introduction to Computer Graphics 03: Basic Principles of 2D Graphics

03: Basic Principles of 2D Graphics. Lecturer Details Dr. Walid Khedr, Ph.D. Email: [email protected] Web: Department of Information

Embed Size (px)

Citation preview

  • Slide 1

03: Basic Principles of 2D Graphics Slide 2 Lecturer Details Dr. Walid Khedr, Ph.D. Email: [email protected] Web: www.staff.zu.edu.eg/wkhedr Department of Information Technology Slide 3 Main Topics Introduction Basic principles of two-dimensional graphics Drawing lines and curves Areas, text and colors Basic principles of three-dimensional graphics Modeling three-dimensional objects Visible surface determination Illumination and shading Special effects and virtual reality Slide 4 Basic Principles of 2D Graphics 1. Objectives 2. Representing an Image Vector and Raster Graphics 3. Getting Started with Java 2D 4. Basic Geometric Objects 5. Geometric Transformations scaling, rotation, shearing and translation. 6. Homogeneous Coordinates 7. Moving Objects 8. Interpolators Slide 5 Objectives Learn basic concepts that are required for the understanding of two-dimensional graphics. Distinguish between the representation of images on output devices and the model of the image itself. Learn the basic geometric objects and transformations Slide 6 Representing an Image Original ImageVector graphics: Representation by basic geometric objects (lines, circles, ellipses, cubic curves,..). Raster graphics: Representation in the form of a pixel matrix. Slide 7 Raster Graphics Raster graphics: is the representation of images as an array of pixels. Colors are assigned to each pixel. Used for computer monitors, printers, and file formats like bitmap or jpg. Slide 8 Storing Bitmap Images Frequently large files Example: 600 rows of 800 pixels with 1 byte for each of 3 colors ~1.5MB file File size affected by Resolution (the number of pixels per inch) Amount of detail affecting clarity and sharpness of an image Levels: number of bits for displaying shades of gray or multiple colors Palette: color translation table that uses a code for each pixel rather than actual color value Data compression Slide 9 Vector Graphics Vector graphics is the use of geometrical primitives such as points, lines, curves, and shapes or polygon(s), which are all based on mathematical equations, to represent images. Efficient storage Scalable Slide 10 Slide 11 Raster or Vector ? Computer monitors, printers and also various formats for storing images are based on raster-oriented graphics, also called pixel-oriented graphics. In order to display vector-oriented graphics in the form of raster graphics, all geometrical shapes must be converted into pixels. Scan Conversion Lead to high computational efforts Possible aliasing effects Slide 12 Aliasing Effect (Jagged Edges) Slide 13 Getting Started with Java 2D Before modelling of two-dimensional objects is discussed in more detail, a short introduction into how Java 2D can be used to generate images in general is provided. Slide 14 AWT AWT is a java package that we will be using in order to create graphical user interfaces Some important classes within the AWT package Containers: Frame: has an title bar, can contain many things Panel Each of the above containers has a paint method that we will inherit but will usually override when we want to customize the containers graphics. Slide 15 AWT Example Slide 16 Paint Method paint called automagically when needed applet/application starts/resizes/unhidden/etc Paint method takes a Graphic object as an argument. This argument is passed to the paint method when drawing operation is required Slide 17 Java 2D & Paint Method The class Graphics2D within Java 2D extends the class Graphics. In order to exploit the options of Java 2D, the Graphics object must be casted into a Graphics2D object within the paint() method. In order to keep the example programs as simple and understandable as possible, the computations required for defining and positioning the geometric objects are carried out directly in the paint method. Slide 18 Java 2D & Paint Method, Cont. Slide 19 Window Coordinates Slide 20 Basic Geometric Objects The basic geometric objects in computer graphics are usually called primitives or graphics output primitives. The basic primitives are the following ones: Points that are uniquely defined by their x- and y-coordinate. Their main function is the description of other objects like lines Lines, polylines or curves can be defined by two or more points. For a line two points are needed curves require additional control points. Polylines are connected sequences of lines. Areas are usually bounded by closed polylines or polygons. Areas can be filled with a color or a texture. Slide 21 Polygons Polygon, closed polyline: The last line segment of a polyline ends where the first line segment started. Non-self-overlapping Convexity: Whenever two points are within the polygon the connecting line between these two points lies completely inside the polygon as well. Slide 22 Parametric Curves Parametric curves are defined as parametric polynomials Quadratic Curves: Two endpoints and one control point. Cubic Curves: Two endpoints and two control points The curve begins and ends in the two specified endpoints. In general, it will not pass through control points. The control points define the direction of the curve in the two endpoints. Slide 23 Quadratic Curve Example Slide 24 Cubic Curve Example Slide 25 Parametric curves When fitting quadratic or cubic curves together it is not sufficient to simply use the endpoint of the previous curve as a starting point for the next curve. In order to avoid sharp bends, the first control point of a succeeding curve must be on the line defined by the last control and endpoint of the previous curve. Other important curves in computer graphics are circles and ellipses Slide 26 Definition of Areas Polygons, circles and ellipses define areas. Areas can be bounded by a closed curve. Areas can be filled by colors and textures. Slide 27 Set-Theoretic Operations Modifying the shape of elementary geometric objects by geometric transformations. Application of set-theoretic operations like union, intersection, difference and symmetric difference to previously defined areas. Union, intersection, difference and symmetric difference of a circle and a rectangle. Slide 28 Basic Geometric Objects in Java 2D The abstract class Shape with its various subclasses allows the construction of various two-dimensional geometric objects. Shapes will not be drawn until the draw or the fill method is called with the corresponding Shape as argument in the form graphics2d.draw(shape) or graphics2d.fill(shape), respectively. Slide 29 Basic Geometric Objects in Java 2D The abstract class Point2D is NOT a subclass of Shape. Points cannot be drawn in Java. They are only supposed to be used for the description of other objects. Subclasses of Point2D: Point2D.Float and Point2D.Double. Slide 30 Basic Geometric Objects in Java 2D Line (segment): Two endpoints are needed to define aline. Line2D.Double line = new Line2D.Double(x1,y1,x2,y2); Quadratic curve: Two endpoints and a control point are needed to define a quadratic curve. QuadCurve2D.Double qc = new QuadCurve2D.Double(x1,y1, ctrlx,ctrly, x2,y2); Cubic curve: Two endpoints and two control points are needed to define a cubic curve. CubicCurve2D.Double cc = new CubicCurve2D.Double(x1,y1, ctrlx1,ctrly1, ctrlx2,ctrly2, x2,y2); Slide 31 Basic Geometric Objects in Java 2D General path: A GeneralPath is sequences of lines, quadratic and cubic curves. GeneralPath gp = new GeneralPath(); gp.moveTo(50,50); gp.lineTo(50,200); gp.quadTo(150,500,250,200); gp.curveTo(350,-100,150,150,100,100); gp.lineTo(50,50); Slide 32 General Path Car Slide 33 Slide 34 Slide 35 Slide 36 Slide 37 Slide 38 Slide 39 Slide 40 Slide 41 Slide 42 Slide 43 Slide 44 Rectangles Rectangle: By Rectangle2D.Double r2d = new Rectangle2D.Double( x,y,width,height); A rectangle is defined by left corner has the coordinates (x,y) width width and height height. Slide 45 Circles and Ellipses Circles and ellipses: by Ellipse2D.Double elli = new Ellipse2D.Double(x,y,width,height); An axes-parallel ellipse is defined by a bounding Rectangle with the parameters x,y,width,height. Ellipse2D.Double elli = new Ellipse2D.Double(x-r,y- r,2*r,2*r); defines a circle with centre (x,y) and radius r. Slide 46 Example: Rectangle and Ellipse Slide 47 Ellipse Arcs Arcs (of ellipses) are defined by Arc2D.Double arc = new Arc2D.Double( rect,start,extend,type); rect2D: specifies the bounding rectangle of the corresponding ellipse in the form of a Rectangle2D. Start: is the angle. The angle corresponds to the angle with the x-axis Extend: is the opening angle of the arc, i.e., the arc extends from the start angle start to the angle start + extend. Type: can take one of the three values Arc2D.OPEN, Arc2D.PIE and Arc2D.CHORD Slide 48 Ellipse Arcs Slide 49 Area Objects Slide 50 Geometric Transformations Geometric transformations play a crucial role in computer graphics. Geometric transformations can be used to position objects: To scale them To rotate them To change the shape of objects To move them The most important geometric transformations are scaling, rotation, shearing and translation. Slide 51 Representation of Points in Matrix Form In two-dimensional coordinate system, any point is represented in terms of x and y coordinates. The point (x, y) can be converted into matrix in the following two ways: Slide 52 Representation of 2D Objects in Matrix Form Slide 53 Transformation of Points The result of the multiplication of a matrix (x y) containing the coordinate of a point P and a general 2x2 transformation matrix: Slide 54 Transformation The term transform' means 'to change'. This change can either be of shape, size or position of the object. To perform transformation on any object, object matrix X is multiplied by the transformation matrix T. Slide 55 Scaling Scaling means to change the size of object. This change can either be positive or negative. After applying scaling factor or matrix on any object, coordinate is either increased or decreased depending on the value of scaling factors. Scaling matrix is given by: where, S x and S y are scaling factors in x-direction and y- direction respectively. Slide 56 Scaling, Cont. Slide 57 Scaling Example Slide 58 General Fixed-Point Scaling Applying a scaling to an object that is not centered around the origin of the coordinate system will lead to a translation in addition to the scaling. But sometimes it may be required to perform scaling without altering the object's position. Translate Object so that it is centered around the origin, perform scaling then reverse the translation. Slide 59 Reflection If S x is negative, in addition to scaling in the x- direction, a reflection with respect to the y-axis is applied. If S Y is negative, in addition to scaling in the y- direction, a reflection with respect to the x-axis is applied. If S x or S y equal -1, only reflection is applied. Slide 60 Rotation Slide 61 Derivation of the Rotation Matrix Slide 62 Rotation Example 1 Slide 63 Rotation Example 2 Slide 64 Shearing The shear transformation is an elementary geometric transformation that causes a certain deformation of objects. Shearing can be done either in x-direction or y- direction. The off-diagonal terms in transformation matrix is responsible for shearing. Hence the matrix of shearing is: Slide 65 Shearing Example Slide 66 Translation A translation T(d x, d y ) causes a shift by the vector d = (d x, d y ) T. This means the translation maps the point (x, y) to the point: Slide 67 Translation, Cont. Slide 68 Slide 69 Homogeneous Coordinates To produce a sequence of transformation with above equations, we must calculate the transformed coordinates one step at a time. A more efficient approach is to combine sequence of transformations into one transformation In order to combine sequence of transformation we use Homogeneous Coordinates Slide 70 Homogeneous Coordinates In order to compute transformations based on matrix multiplications, homogeneous coordinates are introduced. The homogeneous coordinates of non-homogeneous vector [x y] are [x' y' h]. where x = x'/h and y = y/h and where h is any real number. One set of homogeneous coordinates is always of the form [x y 1]. We select this form to represent the position vector [x y] in physical xy-plane systems. There is no unique homogeneous coordinate representation i.e., [8 4 2]. [ 12 6 3], [16 8 4] all represent the physical point (4, 2), Slide 71 Homogeneous Coordinates The general 2D-Transformation matrix is now 3x3, i.e. Translation in homogeneous coordinates will be Slide 72 Transformation Matrices Slide 73 Combined Transformations The composition of geometric transformations can be computed by matrix multiplication in homogeneous coordinates. Matrix multiplication is noncommutative. The order in which geometric transformations are applied matters. Changing the order might lead to a different result. It should also be taken into account that transformations in matrix notation or as compositions of mappings are carried out from right to left Slide 74 Combined Transformations Slide 75 Application of Transformation Slide 76 Application of Transformation, Cont. The required transformation is Slide 77 Geometric Transformations in Java 2D Slide 78 Slide 79 Slide 80 Moving Objects Moving objects (animated graphics) can be implemented based on geometric transformations. The transformations must describe the movement of the object(s): The object is modeled by suitable geometric transformations The object must be drawn The objects position in the next frame is computed based on transformations. The old object (or the whole window) is deleted. The updated object and the background are drawn again. Slide 81 Moving Objects Example A moving clock with a single hand for the seconds is considered sliding from the lower left to the upper right of a display window. The clock itself consists of a quadratic frame and the single rectangular hand for the seconds. A translation is needed in order to move the quadratic frame of the clock from the lower left to the upper right corner of the window. This translation must also be applied to the hand for the seconds. In addition to the translation, the hand must also rotate. Slide 82 Moving Objects Example, Cont. Slide 83 Slide 84 Method 1: Keep track of the position, orientation and scaling of the object and apply the transformations step by step to the already transformed object. Method 2: Generate an object in the origin and keep track of the accumulated transformations defining the movement. Always apply the accumulated transformations to the object in the origin of the coordinate system in a suitable order. Slide 85 Moving Objects Example, Cont. Slide 86 Interpolators Is another ways to model movements or changes of objects in animated graphics. Aim: Continuous transition from an initial state to final state. Simplest case: moving from position (point) p 0 to p 1 The points p on the line between p 0 and p 1 are the convex combinations of these two points given by: Slide 87 Interpolation of Transformations Slide 88 Interpolation of Transformations, Cont. Slide 89 Simple Object Interpolation Slide 90 Simple Object Interpolation, Cont. Both Letters C and D are described by two quadratic curves Slide 91 Reading Chapter 2 Slide 92 Next Lecture Scan conversion Drawing lines and curves Slide 93 Thank You