⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphics.txt

📁 书名:C语言科学与艺术,以前交钱下载的
💻 TXT
📖 第 1 页 / 共 2 页
字号:
Similarly, it is also usually a simple matter to create a new window onthe screen to use for drawing.  Existing graphics libraries usually havehigh-level tools for window creation, so the InitGraphics implementationneed only perform the appropriate set of function calls to create andposition a new window.The hard part is implementing the conceptual model used by graphics.h,which is fundamentally different from that used in existing graphicslibraries.  In the typical graphics library supplied with a compiler oroperating system, applications are coded using what is generally calledthe event loop strategy.  Each application establishes an initial stateand then enters a loop with the following paradigmatic form:          void EventLoop(void)          {              EventT event;              while (event = WaitForNextEvent()) {                  RespondToEvent(event);              }          }Calls to WaitForNextEvent block until a system event occurs, at whichpoint the operating system delivers the event to the application, whichthen initiates some appropriate response.  These events include, forexample, requests to update the window, mouse clicks, and keyboardactivity.While the event-loop paradigm is appropriate for experiencedprogrammers, it makes little sense to students in an introductorycourse.  Those students are used to writing a function called main thatexecutes sequentially, one statement at a time.  In the first few weeksof a CS1 course, it is unwise to introduce a different paradigm.  Thus,students should be able to code graphical applications that start at thebeginning of main and execute sequentially.  For example, to draw aone-inch square centered at the point (1, 1), introductory studentsexpect the program to look like this:          main()          {              InitGraphics();              MovePen(0.5, 0.5);              DrawLine(0, 1);              DrawLine(1, 0);              DrawLine(0, -1);              DrawLine(-1, 0);          }This code contains no event loop and has no place for one.  Thus, thechallenge of implementing the graphics library is to find a way to hidethe complexity of the event loop processing inside the libraryimplementation.Fortunately, this problem is not usually as hard as it appears becausemost C programming environments already include a solution as part ofthe standard I/O library implementation.  Like the graphics modelassumed by the graphics.h interface, the I/O model assumed by stdio.h isnot directly compatible with the event-loop paradigm.  For anapplication that uses stdio.h to work, there must be an event loopsomewhere that intercepts the keyboard events as they occur.  From theperspective of a client of stdio.h, however, the event loop isinvisible; the program simply calls an input function, such as getcharor scanf, which eventually returns with the desired input characters.To preserve the structural simplicity offered by stdio.h,microcomputer-based implementations of C typically implement thestandard I/O library so that the required event-loop processing isembedded in the console input routines.[1]  Whenever the program waitsfor input from the standard input device, the library implementationsimply enters an event loop, during which it responds to any systemevents that occur.  Keyboard input is queued until a complete line canbe returned to the application, but the event loop also processes updaterequests, so that the program behaves correctly if windows arerepositioned on the screen.The fact that the system must already perform the required event-loopprocessing makes it easier to implement the graphics library.  In mostcases, all that is needed is to make the existing event loop used forconsole I/O also respond to update events for the graphics window.  Thiseffect can be achieved in a variety of ways, as illustrated by thefollowing examples:   o  In the Borland C environment for Microsoft Windows, making the      graphics window a child of the console window was sufficient      to solve the problem, because the parent window automatically      delivers events to its children.   o  In the THINK C environment on the Macintosh, it was necessary      to make a dynamic patch to the console event loop so that it      also called the appropriate update procedures in the graphics      library.   o  The X Windows implementation for Unix required a different      approach.  In this implementation, the function InitGraphics      calls fork to create two parallel threads, connected by a      communication pipe.  One of those threads returns to execute      the application code.  The other enters an event loop waiting      either for X events that require a response or for commands      from the application program sent over the communication pipe.In addition to these platform-specific implementations, the publicdistribution site on aw.com also includes a fully standard version thatdoes not actually display a graphical image but instead writes aPostScript file suitable for printing.  The existence of thisimplementation ensures that it is always possible to use the simplegraphics.h interface even on platforms for which no specializedimplementation exists.5.  CONCLUSIONSOur experience over the last two years at Stanford suggests that using agraphics library in an introductory course enhances student interest andhelps reinforce several essential programming concepts.  We have alsodemonstrated that it is possible to design a graphics library that canbe implemented in a relatively portable way.REFERENCES[Hilburn93] Thomas B. Hilburn, "A top-down approach to teaching anintroductory computer science course," SIGCSE Bulletin, March 1993.[House94] Donald House and David Levine, "The art and science ofcomputer graphics:  a very depth-first approach to the non-majorscourse," SIGCSE Bulletin, March 1994.[Papert80] Seymour Papert, Mindstorms, New York: Basic Books, 1980.[Roberge92] James Roberge, "Creating programming projects with visualimpact," SIGCSE Bulletin, March 1992.[Roberts93]  Eric S. Roberts, "Using C in CS1: Evaluating the Stanfordexperience," SIGCSE Bulletin, March 1993.[Roberts95]  Eric S. Roberts, The Art and Science of C: A Library-BasedApproach, Reading, MA: Addison-Wesley, 1995.FOOTNOTES[1] In THINK C, for example, this operation is performed as part of theimplementation of the console I/O package console.c.  In the BorlandC/C++ system, the same functionality is provided by the EasyWin package.Figure 1.  Functions in graphics.hInitGraphics();              This procedure initializes the package and                             creates the graphics window on the screen.                             The call to the InitGraphics function is                             typically the first statement in main.MovePen(x, y);               This procedure moves the current point to                             the position (x, y) without drawing a line.DrawLine(dx, dy);            This procedure draws a line extending from                             the current point by moving the pen dx and                             dy inches along the respective coordinate                             axes.DrawArc(r, start, sweep);    This procedure draws a circular arc, which                             always begins at the current point.  The                             arc itself has radius r, and starts at the                             angle specified by the parameter start,                             relative to the center of the circle.  This                             angle is measured in degrees                             counterclockwise from the 3 o'clock                             position along the x-axis, as in                             traditional mathematics.  The fraction of                             the circle drawn is specified by the                             parameter sweep, which is also measured in                             degrees.  If the value of sweep is                             positive, the arc is drawn counterclockwise                             from the current point; if sweep is                             negative, the arc is drawn clockwise.  The                             current point at the end of the DrawArc                             operation is the final position of the pen                             along the arc.width = GetWindowWidth();    These functions return the width andheight = GetWindowHeight();  height, in inches, of the drawing area of                             the graphics window.cx = GetCurrentX();          These functions return the current x- andcy = GetCurrentY();          y-coordinates of the pen.  Like all                             coordinates in the graphics package, these                             values are measured in inches relative to                             the origin.Figure 2.  Selected functions in extgraph.hDrawEllipticalArc            This procedure is similar to DrawArc in the  (rx, ry, start, sweep);    basic graphics library, but draws an                             elliptical rather than a circular arc.StartFilledRegion(density);  These procedures are used to draw a filledEndFilledRegion();           region.  The boundaries of the region are                             defined by a set of calls to the line and                             arc drawing functions that represent the                             boundary path.  The parameter density is a                             floating-point value that represents the                             pixel density to be used for the fill                             color.DrawTextString(s);           This procedure draws the text string s at                             the current pen position.  The pen position                             is moved so that it follows the final                             character in the string.  The interface                             also contains functions to set the font,                             size, and style of the text.SetPenColor(color);DefineColor(name, r, g, b, );                             The SetPenColor function sets the graphics                             package to draw in some color other than                             the default black.  Colors are identified                             by name, which makes it easier to save and                             restore the current color.  By default,                             only a small set of standard colors are                             defined, but DefineColor allows clients to                             define new ones by specifying their RGB                             values.x = GetMouseX();             These functions make it possible for they = GetMouseY();             client to use the mouse to design graphicalflag = MouseButtonIsDown();  user interfaces.  Given these primitives,                             it is easy to design interfaces that                             provide portable implementations of                             buttons, menus, and other standard                             interactors.Pause(seconds);              The Pause function forces the system to                             redraw the screen and then waits for the                             specified number of seconds.  This function                             can be used to animate the displays.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -