Laboratory Exercise 5

Similar documents
Laboratory Exercise 8

EECS150 - Digital Design Lecture 12 - Video Interfacing. MIPS150 Video Subsystem

EECS150 - Digital Design Lecture 15 - Video

Line Rasterization and Antialiasing

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

CSci 1113, Fall 2015 Lab Exercise 11 (Week 13): Discrete Event Simulation. Warm-up. Stretch

Old 257 Exam #2s for Practice

EECS150 - Digital Design Lecture 15 - Project Description, Part 5

Quick Guide for Photoshop CC Basics April 2016 Training:

Decision Making and Loops

COMP 2718: The OS, Shell, Terminal, and Text

Tutorial 3: Constructive Editing (2D-CAD)

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CISC 1600 Lecture 3.1 Introduction to Processing

MAZE A SIMPLE GPU WITH EMBEDDED VIDEO GAME. Designed by:

Mini-Project 1: The Library of Functions and Piecewise-Defined Functions

Othello Game. First, to learn the rules of Othello and play online, you can go here:

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Case Study 1: Piezoelectric Rectangular Plate

ECET 264 C Programming Language with Applications. C Program Control

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Appendix 1: Manual for Fovea Software

Computer Graphics: Graphics Output Primitives Line Drawing Algorithms

Flow Control. CSC215 Lecture

CS1950U Setup Spring 2018

Lecture 3. More About C

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Iteration in Programming

(Refer Slide Time: 00:02:00)

CpSc 101, Fall 2015 Lab7: Image File Creation

Solution Notes. COMP 151: Terms Test

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Basic Elements of C. Staff Incharge: S.Sasirekha

from Appendix B: Some C Essentials

ECB Digital - Way to Go! (7th Grade)

Output Primitives Lecture: 3. Lecture 3. Output Primitives. Assuming we have a raster display, a picture is completely specified by:

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Chapter - 2: Geometry and Line Generations

RASTERIZING POLYGONS IN IMAGE SPACE

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut

LESSON 4. The DATA TYPE char

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Data Types and Variables in C language

Lesson 4 Customize the ToolBox

Scan Conversion. Lines and Circles

L E S S O N 2 Background

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

1. (10 pts) Order the following three images by how much memory they occupy:

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Programming and Data Structures

Introduction. C provides two styles of flow control:

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

ECB Digital - Our World (5th Grade)

Graphics Overview ECE2893. Lecture 19. ECE2893 Graphics Overview Spring / 15

MATLAB Project: Getting Started with MATLAB

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Operating Systemss and Multicore Programming (1DT089)

1 Getting started with Processing

ISETL Graphics Guide. Using the Graphics Facility

Pace University. Fundamental Concepts of CS121 1

UNIT 2 GRAPHIC PRIMITIVES

Multiple Choice Questions ( 1 mark)

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

CS113: Lecture 4. Topics: Functions. Function Activation Records

1 General Principles. General Principles. In this chapter 1-1

Chapter 15 Grade Rule Grading

CSci 1113, Fall 2018 Lab Exercise 11 (Week 13): Graphics. Warm-up

12. Digitizer Introduction. Digitizer

Objectives. Learn how to read and write from memory using C-pointers Implement the very first TOS functions

easel Manager Danielle Crosswell dac2182 Language Guru Tyrus Cukavac thc2125 System Architect Yuan-Chao Chou yc3211 Tester Xiaofei Chen xc2364

CMPE-013/L. Introduction to C Programming

QuickTutor. An Introductory SilverScreen Modeling Tutorial. Solid Modeler

Introduction to the Turing Programming Language program commands run Exercise #1 not var put get put Programming Names File/Save As..

Introduction to CS Dealing with tables in Word Jacek Wiślicki, Laurent Babout,

Vision Pointer Tools

CMSC 412 Project 1: Keyboard and Screen Drivers

Lesson 18: Animation. Computer Programming is Fun!

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

EECS 388 C Introduction. Gary J. Minden August 29, 2016

DIGITAL OSCILLOSCOPE. Ghassan Hamarneh

Lab Instructor : Jean Lai

Introduction to Computer Science Midterm 3 Fall, Points

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Chapter 3 - Using Arduino-Like Functions Page 55

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

CHAOS Chaos Chaos Iterate

1 Some easy lines (2, 17) (10, 17) (18, 2) (18, 14) (1, 5) (8, 12) Check with a ruler. Are your lines straight?

Helios Support for 2-D Block Moves

Overview of Computer Graphics

C: How to Program. Week /Mar/05

Generalised User Interface for Embedded Applications using an LCD screen and keypad.

Maya Lesson 8 Notes - Animated Adjustable Desk Lamp

We will start our journey into Processing with creating static images using commands available in Processing:

Programme. Introduction to Animation Shop

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Introduction to C# Applications

Transcription:

Laboratory Exercise 5 Using ASCII Graphics for Animation The purpose of this exercise is to learn how to perform simple animations under Linux. We will use the ARM A9 processor, in the DE1-SoC Computer. Graphics will be displayed by sending special commands to a Linux Terminal window. Part I A Linux Terminal window is normally used to display ASCII text. But the Terminal also supports a number of special commands that can be used to make simple drawings, often called ASCII graphics. You can use commands to clear the screen, move the Terminal cursor to specific locations, show/hide the cursor, change the color of text, and so on. An example of a program that uses ASCII graphics is given in Figure 1. 1 /* This program draws a few characters on the screen. */ 2 #include <stdio.h> 3 #define YELLOW 33 4 #define CYAN 36 5 #define WHITE 39 6 7 void plot_pixel(int, int, char, char); 8 9 int main(void) { 10 char c; 11 printf ("\e[2j"); // clear the screen 12 printf ("\e[?25l"); // hide the cursor 13 14 plot_pixel (1, 1, CYAN, * ); 15 plot_pixel (80, 24, CYAN, * ); 16 plot_pixel (40, 12, YELLOW, X ); 17 18 c = getchar (); // wait for user to press return 19 printf ("\e[2j"); // clear the screen 20 printf ("\e[%2dm", WHITE); // reset foreground color 21 printf ("\e[%d;%dh", 1, 1); // move cursor to upper left 22 printf ("\e[?25h"); // show the cursor 23 fflush (stdout); 24 } 25 26 void plot_pixel(int x, int y, char color, char c) 27 { 28 printf ("\e[%2dm\e[%d;%dh%c", color, y, x, c); 29 fflush (stdout); 30 } Figure 1: An example of code that uses ASCII graphics. The first line of code in Figure 1 includes the stdio.h library, which is required to use the printf function. The next three lines define constants, described later, for text colors which can be used in the Terminal window. A command is sent to the Terminal in line 11 by using printf. All Terminal window commands begin with the ASCII ESC (escape) character, which is specified in the printf string using the syntax \e. The command in line 11, 1

which is [2J, instructs the Terminal to clear the screen. Another command, [?25l, given in line 12, causes the Terminal to hide the cursor so that it is not visible to the user. Next, the function plot_pixel is called to draw three characters at specific locations on the screen. In this example the size of the Terminal window is 80 columns by 24 rows. Coordinates (1, 1) are at the top-left corner of the screen and (80, 24) are at the bottom-right corner. The calls to plot_pixel in lines 14 to 16 draw cyan-colored * characters at coordinates (1, 1) and (80, 24), and a yellow X in the middle of the screen. The plot_pixel function, shown in lines 26 to 30 uses two commands to draw a character. The first command is [nnm, where nn is called an attribute. The attribute can be used to set the color of text characters, by using different values of nn. Examples of color attributes are nn = 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (magenta), 36 (cyan), and 37 (white). The second command in plot_pixel is [rr;cch, where rr and cc specify a row and column on the screen, respectively. This command moves the Terminal cursor to (x, y) coordinates (cc, rr). In line 18 the program waits, using the getchar function, for the user to press a key. Finally, commands are sent to the Terminal to clear the screen, set the text-color to white, set the cursor to coordinates (1, 1), and to show the cursor. The ASCII graphics commands described above became popular around the year 1980 when they were introduced in computer video terminals called the VT100, manufactured by Digital Equipment Corporation. The Linux Terminal provides ASCII graphics by emulating the capabilites of the VT100 video terminal. More information about ASCII graphics commands can be found by searching on the Internet for a topic such as VT100 graphics. Perform the following 1. Type the code in Figure 1 into a file. 2. Compile your program and execute it in a Linux Terminal window to display the ASCII graphics. Part II In this part you will learn how to implement a simple line-drawing algorithm. Drawing a line on a screen requires coloring pixels between two coordinates (x 1, y 1 ) and (x 2, y 2 ), such that the pixels represent the desired line as closely as possible. In the case of ASCII graphics, a pixel represents a character coordinate on the screen. Consider the example in Figure 2, where we want to draw a line between coordinates (1, 1) and (12, 5). The squares in the figure represent the location and size of pixels on the screen. As indicated in the figure, we cannot draw the line precisely we can only draw a shape that is similar to the line by coloring the pixels that fall closest to the line s ideal location on the screen. Figure 2: Drawing a line between coordinates (1, 1) and (12, 5). We can use algebra to determine which pixels to color. This is done by using the end points and the slope of the line. The slope of our example line is slope = (y 2 y 1 )/(x 2 x 1 ) = 4/11. Starting at point (1, 1) we move along the x axis and compute the y coordinate for the line as follows: 2

y = y 1 + slope (x x 1 ) Thus, for column x = 2, the y location of the pixel is 1 + 4 11 (2 1) = 1 4 11. Since pixel locations are defined by integer values we round the y coordinate to the nearest integer, and determine that in column x = 2 we should color the pixel at y = 1. For column x = 3 we perform the calculation y = 1 + 4 11 (3 1) = 1 8 11, and round the result to y = 3. Similarly, we perform such computations for each column between x 1 and x 2. The approach of moving along the x axis has drawbacks when a line is steep. A steep line spans more rows than it does columns, and hence has a slope with absolute value greater than 1. In this case our calculations will not produce a smooth-looking line. Also, in the case of a vertical line we cannot use the slope to make a calculation. To address this problem, we can alter the algorithm to move along the y axis when a line is steep. With this change, we can implement a line-drawing algorithm known as Bresenham s algorithm. Pseudo-code for this algorithm is given in Figure 3. The first 15 lines of the algorithm make the needed adjustments depending on whether or not the line is steep. Then, in lines 17 to 22 the algorithm increments the x variable 1 step at a time and computes the y value. The y value is incremented when needed to stay as close to the ideal location of the line as possible. Bresenham s algorithm calculates an error variable to decide whether or not to increment each y value. The version of the algorithm shown in Figure 3 uses only integers to perform all calculations. To understand how this algorithm works, you can read about Bresenham s algorithm in a textbook or by searching for it on the Internet. 1 draw_line(x0, x1, y0, y1) 2 3 boolean is_steep = (abs(y1 - y0) > abs(x1 - x0)) 4 if is_steep then 5 swap(x0, y0) 6 swap(x1, y1) 7 if x0 > x1 then 8 swap(x0, x1) 9 swap(y0, y1) 10 11 int deltax = x1 - x0 12 int deltay = abs(y1 - y0) 13 int error = -(deltax / 2) 14 int y = y0 15 if y0 < y1 then y_step = 1 else y_step = -1 16 17 for x from x0 to x1 18 if is_steep then draw_pixel(y,x) else draw_pixel(x,y) 19 error = error + deltay 20 if error 0 then 21 y = y + y_step 22 error = error - deltax Figure 3: Pseudo-code for a line-drawing algorithm. 1. Write a C program that uses Bresenham s line-drawing algorithm to draw a few lines on the screen. An example of output that your program might produce is illustrated in Figure 4. In this example the lines are drawn using the * character, but you can use any ASCII character to draw your lines. 2. Compile and test your program using a Linux Terminal. 3

Figure 4: Drawing a few lines on the screen. Part III Animation is an exciting part of computer graphics. Moving a displayed object is an illusion created by showing this same object at different locations on the screen. A simple way to move an object is to first draw the object at one position, and then after a short time erase the object and draw it again at another nearby position. To realize animation it is necessary to move objects at regular time intervals. For this exercise we can use a timer controlled by the Linux kernel, such as the nanosleep function. A reasonable delay might be about 0.1 seconds, but you should experiment with different animation delays and observe the results. Documentation for nanosleep can be obtained by searching on the Internet. 1. Write a C-language program that moves a horizontal line up and down on the screen and bounces the line off the top and bottom edges of the display. Your program should first clear the screen and then draw the line at a starting row. Then, in an endless loop you should erase the line, and redraw it one row above or below the last one. When the line reaches the top, or bottom, of the screen it should start moving in the opposite direction. 2. Compile your code and test it. Experiment with different animation delay times. Part IV Having gained the basic knowledge about displaying images and animations, you can now create a more interesting animation. You are to create an animation of five objects on the screen. These objects should appear to be moving continuously and bouncing off the edges of the screen. The objects should be connected with lines to form a chain. An illustration of the animation is given in Figure 5. Part a of the figure shows one position of the objects with arrows that indicate the directions of movement, and Figure 5b shows a subsequent position of the objects. In each step of your animation each of the objects should appear to move on a diagonal line: up/left, up/right, down/left, or down/right. Move the objects one row and one column at a time on the Terminal screen. 4

(a) (b) Figure 5: Two instants of the animation. To make the animation look slightly different each time you run it, use the C library function rand to help calculate initial positions for each of the objects, and to determine their directions of movement. 1. Write a C-language program to implement your animation. Use some ASCII character to represent each object for example, the X character. Connect the objects together by drawing lines using other characters for example *, -, or. Use Bresenham s algorithm, described in Part II, to draw the lines. An example of a suitable main program is given in Figure 6. The code first initializes some variables, sets the locations of the objects to be drawn, and sets the animation time. The code then enters an infinite loop, which can be interrupted by pressing ˆC on the keyboard. In each iteration of the loop the code calls a function to draw the animation. The draw function first clears the screen, draws the objects and lines, and then updates the locations of objects. At the bottom of the loop the code calls the nanosleep function to wait for the animation time to expire. 2. Compile your program and test the animation. Note that, depending on the quantity of text being shown on the screen, your animation may look smoother if the connection between your host computer and the DE1-SoC Computer has a high bandwidth; for example, a network connection may provide better results in comparison to a serial-port connection. Part V For this part of the exercise you are to enhance the animation from Part IV so that during the animation the following changes can take place: 1. The speed of movement of the objects can be increased or decreased 2. The number of objects in the animation can be increased or decreased 3. The lines between objects can be drawn or not drawn 1. Implement the speed control discussed above for the animation. The speed of animation should be increased by some amount, say 10%, when you press the pushbutton switch KEY 0, and decreased by the same amount 5

when you press KEY 1. Pressing KEY 2 should reduce the number of displayed objects by one, and pressing KEY 3 should increase the number of objects. Finally, when any of switches SW 9 0 is set to the on position the lines between objects should not be drawn; only when all SW switches are set to the off position should the lines appear. You can read the KEY and SW switch settings using either memory-mapped I/O or device drivers. Character device drivers for these switches were developed in Laboratory Exercise 3. 2. Compile your code and test the new features for changing the animation. 3. Add any other animation features that you may find interesting. 1... include files (not shown) 2 3 volatile sig_atomic_t stop; // used to exit the program cleanly 4 void catchsigint(int); 5 struct timespec animation_time; // used to control the animation timing 6 7... declare function prototypes and variables (not shown) 8 9 /******************************************************************************* 10 * This program draws Xs and lines on the screen, in an animated fashion. 11 ******************************************************************************/ 12 int main(void) 13 { 14... declare variables (not shown) 15 16 // catch SIGINT from ^C, instead of having it abruptly close this program 17 signal(sigint, catchsigint); 18 19 // set random initial position, "delta", and color for all Xs 20... code not shown 21 22 printf ("\033[?25l"); // hide the cursor 23 fflush (stdout); 24 25 // initialize the animation speed 26 animation_time.tv_sec = 0; 27 animation_time.tv_nsec = 200000000; // 0.2 seconds 28 while (!stop) 29 { 30 draw ( ); // draw the animation 31 nanosleep (&animation_time, NULL); // wait for timer 32 } 33 printf ("\033[2J"); // clear the screen 34 printf ("\e[%2dm\e[%d;%dh", WHITE, min_x, min_y); // reset color and cursor 35 printf ("\e[?25h"); // show the cursor 36 return 0; 37 } 38 39 /* Function to allow clean exit of the program */ 40 void catchsigint(int signum) 41 { 42 stop = 1; 43 } Figure 6: Main program for Part IV. Copyright c Intel Corporation. 6