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

📄 graphics.c

📁 书名:C语言科学与艺术,以前交钱下载的
💻 C
📖 第 1 页 / 共 3 页
字号:
        FrameOval(&rect);    } else {        dt = atan2(InchesY(1), MaxF(fabs(rx), fabs(ry)));        mint = Radians(start);        maxt = Radians(start + sweep);        for (t = mint; t < maxt; t += dt) {            if (t > maxt - dt / 2) t = maxt;            ix0 = ScaleX(x + rx * cos(t));            iy0 = ScaleY(y + ry * sin(t));            if (t == mint) {                MoveTo(ix0, iy0);            } else {                LineTo(ix0, iy0);            }        }    }    SetArcBB(&rect, x, y, rx, ry, start, sweep);    UpdateRect(&rect);    SetPort(saveWindow);}/* * Function: DisplayText * Usage: DisplayText(x, y, text); * ------------------------------- * Displays the specified text starting at the point (x, y) * using the current font, size, and style parameters. */static void DisplayText(double x, double y, string text){    GrafPtr saveWindow;    Rect rect;    int ix, iy;    ix = ScaleX(x);    iy = ScaleY(y);    GetPort(&saveWindow);    SetPort((WindowPtr) osWindow);    PrepareToDrawText();    SetTextBB(&rect, x, y, text);    UpdateRect(&rect);    MoveTo(ix, iy);    DrawText(text, 0, strlen(text));    SetPort(saveWindow);}/* * Function: SetArcBB * Usage: SetArcBB(&rect, xc, yc, rx, ry, start, sweep); * ----------------------------------------------------- * This function sets the rectangle dimensions to the bounding * box of the arc segment specified by the remaining arguments. */static void SetArcBB(Rect *rp, double xc, double yc,                      double rx, double ry, double start, double sweep){    int xmax, xmin, ymax, ymin;    int xl, xr, yt, yb;    int ix0, iy0, ix1, iy1;    xmin = ScaleX(xc - rx);    ymin = ScaleY(yc + ry);    xmax = xmin + PixelsX(2 * rx);    ymax = ymin + PixelsX(2 * ry);    if (sweep < 0) {        start += sweep;        sweep = -sweep;    }    if (sweep >= 360) {        SetRect(rp, xmin, ymin, xmax, ymax);        return;    }    if (start < 0) {        start = 360 - fmod(-start, 360);    } else {        start = fmod(start, 360);    }    ix0 = ScaleX(xc + rx * cos(Radians(start)));    iy0 = ScaleY(yc + ry * sin(Radians(start)));    ix1 = ScaleX(xc + rx * cos(Radians(start + sweep)));    iy1 = ScaleY(yc + ry * sin(Radians(start + sweep)));    if (start + sweep > 360) {        xr = xmax;    } else {        xr = Max(ix0, ix1);    }    start = fmod(start + 270, 360);    if (start + sweep > 360) {        yt = ymin;    } else {        yt = Min(iy0, iy1);    }    start = fmod(start + 270, 360);    if (start + sweep > 360) {        xl = xmin;    } else {        xl = Min(ix0, ix1);    }    start = fmod(start + 270, 360);    if (start + sweep > 360) {        yb = ymax;    } else {        yb = Max(iy0, iy1);    }    SetRect(rp, xl, yt, xr + 1, yb + 1);}/* * Function: SetTextBB * Usage: SetTextBB(&rect, x, y, text); * ------------------------------------ * This function sets the rectangle dimensions to the bounding * box of the text string. */static void SetTextBB(Rect *rp, double x, double y, string text){    GrafPtr saveWindow;    FontInfo fInfo;    int ix, iy, width;    GetPort(&saveWindow);    SetPort((WindowPtr) osWindow);    ix = ScaleX(x);    iy = ScaleY(y);    GetFontInfo(&fInfo);    width = TextWidth(text, 0, strlen(text));    SetRect(rp, ix, iy - fInfo.ascent, ix + width, iy + fInfo.descent);    SetPort(saveWindow);}/* * Function: UpdateRect * Usage: UpdateRect(&rect); * ------------------------- * This function sets the invalid area of the screen to include * the update rectangle. */static void UpdateRect(Rect *rp){    GrafPtr saveWindow;    Rect rect;    GetPort(&saveWindow);    SetPort((WindowPtr) gWindow);    rect = *rp;    InsetRect(&rect, -1, -1);    InvalRect(&rect);    SetPort(saveWindow);}/* * Function: PascalString * Usage: pstr = PascalString(cstr); * -------------------------------- * This function generates a dynamically allocated Pascal string * from the corresponding C string. */static StringPtr PascalString(string cstr){    int len;    StringPtr pstr;    len = strlen(cstr);    pstr = GetBlock(len + 2);    strcpy((char *) &pstr[1], cstr);    pstr[0] = len;    return (pstr);}/* * Function: GetFontNumber * Usage: fontNumber = GetFontNumber(fontName); * -------------------------------------------- * This function returns the font number corresponding to a * Macintosh font name. */static int GetFontNumber(string fontName){    StringPtr pstr;    short fnum;    if (StringEqual(fontName, "Default")) fontName = DefaultFont;    GetFNum(pstr = PascalString(fontName), &fnum);    FreeBlock(pstr);    return (fnum);}/* * Function: InitColors * Usage: InitColors(); * -------------------- * This function defines the built-in colors. */static void InitColors(void){    nColors = 0;    DefineColor("Black", 0, 0, 0);    DefineColor("Dark Gray", .35, .35, .35);    DefineColor("Gray", .6, .6, .6);    DefineColor("Light Gray", .75, .75, .75);    DefineColor("White", 1, 1, 1);    DefineColor("Red", 1, 0, 0);    DefineColor("Yellow", 1, 1, 0);    DefineColor("Green", 0, 1, 0);    DefineColor("Cyan", 0, 1, 1);    DefineColor("Blue", 0, 0, 1);    DefineColor("Magenta", 1, 0, 1);}/* * Function: ShouldBeWhite * Usage: if (ShouldBeWhite()) . . . * --------------------------------- * This function returns TRUE if the current color is so * close to white that it should be represented as such * in a black-and-white world.  Note that the function * is always FALSE in the color domain. */static bool ShouldBeWhite(void){    if (colorOK) return (FALSE);    if (colorTable[penColor].red < .9) return (FALSE);    if (colorTable[penColor].blue < .9) return (FALSE);    if (colorTable[penColor].green < .9) return (FALSE);    return (TRUE);}/* * Function: CheckColorMemory * Usage: CheckColorMemory(); * -------------------------- * By default, the THINK C project is too small to support color. * This function checks to see if the problem is the memory size * and gives a message to that effect. */static void CheckColorMemory(void){    if (colorOK) return;    printf("There is not enough memory in this project to create\n");    printf("a color window.  Try increasing the partition size to\n");    printf("1000K by changing the setting in the Set Project Type\n");    printf("dialog of the Project menu.  Also make sure that the\n");    printf("Monitors control panel is set to at least 16 but no more\n");    printf("than 256 colors.\n");}/* * Function: FindColorName * Usage: index = FindColorName(name); * ----------------------------------- * This function returns the index of the named color in the * color table, or -1 if the color does not exist. */static int FindColorName(string name){    int i;    for (i = 0; i < nColors; i++) {        if (StringMatch(name, colorTable[i].name)) return (i);    }    return (-1);}/* * Event handler * ------------- * The trickiest part of the graphics library code is the * following patch system, which adds a call to MyEventHandler * prior to the SystemEvent trap.  This makes it possible for * the same event loop that handles console events to update * the graphics window instead. */static char patchArea[0x20];/* *  00  PEA    $FFF0(A6) *  04  JSR    xxxx.L *  0A  CLR.L  -(A7) *  0C  PEA    $FFF0(A6) *  10  $A9B2 *  12  JMP    yyyy.L *//* * Function: PatchEventHandler * Usage: PatchEventHandler(); * --------------------------- * This call inserts the patch into the event handling code to * call MyEventHandler.  The function first finds the address * of the SystemEvent trap and then replaces it with a jump * to the code fragment shown above, after xxxx is replaced * by the address of MyEventHandler and yyyy by the address * after the SystemEvent trap in the existing code. */static void PatchEventHandler(void){    long trapAddress;    char *patch;    trapAddress = FindSystemTrap(_SystemEvent);    patch = patchArea;    if ((int) patch & 1) patch++;    memcpy(&patch[0x00], (void *) (trapAddress - 4), 4);    *((short *) (&patch[0x04])) = 0x4EB9;    *((long *) (&patch[0x06])) = (long) MyEventHandler;    *((short *) (&patch[0x0A])) = 0x42A7;    memcpy(&patch[0x0C], (void *) (trapAddress - 4), 6);    *((short *) (&patch[0x12])) = 0x4EF9;    *((long *) (&patch[0x14])) = trapAddress + 2;    *((short *) (trapAddress - 4)) = 0x4EF9;    *((long *) (trapAddress - 2)) = (long) patch;}/* * Function: FindSystemTrap * Usage: addr = FindSystemTrap(trap); * ---------------------------------- * This function finds the first occurrence of the specified trap * in the code stream beginning at the input function. */static long FindSystemTrap(short trap){    long ptr, base;    int i;    base = (long) stdin->proc;    if (*((short *) base) == 0x4EF9) {        base = *((long *) (base + 2));    }    for (ptr = base; *((short *) ptr) != trap; ptr -= 2);    return (ptr);}/* * Function: MyEventHandler * Usage: MyEventHandler(); * ------------------------ * This function catches update events for the console window. */static void MyEventHandler(EventRecord *ep){    int clickType;    WindowPeek window;    switch (ep->what) {      case updateEvt:        if ((WindowPeek) ep->message == gWindow) DoUpdateDisplay();        break;      case mouseDown:          mouseState = TRUE;        clickType = FindWindow(ep->where, (WindowPtr *) &window);        if (window != gWindow) return;        if (clickType == inDrag) {            DragWindow((WindowPtr) window, ep->where,                       &(*GetGrayRgn())->rgnBBox);        }        break;      case mouseUp:          mouseState = FALSE;    }}/* * Function: DoUpdateDisplay * Usage: DoUpdateDisplay(); * ------------------------- * This function copies the bits from the offscreen memory to * the invalid area of the display. */static void DoUpdateDisplay(void){    GrafPtr saveWindow;    Rect rect;    int width, height;    BeginUpdate((WindowPtr) gWindow);    GetPort(&saveWindow);    SetPort((WindowPtr) gWindow);    CopyBits(&((GrafPtr) osWindow)->portBits,             &((GrafPtr) gWindow)->portBits,             &((GrafPtr) osWindow)->portRect,             &((GrafPtr) gWindow)->portRect,             srcCopy, NULL);    SetPort(saveWindow);    EndUpdate((WindowPtr) gWindow);}/* * Utility functions * ----------------- * This section contains several extremely short utility functions * that improve the readability of the code. *//* * Function: StringMatch * Usage: if (StringMatch(s1, s2)) . . . * ------------------------------------- * This function returns TRUE if two strings are equal, ignoring * case distinctions. */static bool StringMatch(string s1, string s2){    register char *cp1, *cp2;    cp1 = s1;    cp2 = s2;    while (tolower(*cp1) == tolower(*cp2)) {        if (*cp1 == '\0') return (TRUE);        cp1++;        cp2++;    }    return (FALSE);}/* * Functions: RectWidth, RectHeight * Usage: w = RectWidth(&r); *        h = RectHeight(&r); * -------------------------------- * These functions return the width and height of a rectangle. */static int RectWidth(Rect *rp){    return (rp->right - rp->left);}static int RectHeight(Rect *rp){    return (rp->bottom - rp->top);}/* * Functions: SetRectFromSize * Usage: SetRectFromSize(&r, x, y, width, height); * ------------------------------------------------ * This function is similar to SetRect except that it takes width * and height parameters rather than right and bottom. */static void SetRectFromSize(Rect *rp, int x, int y, int width, int height){    SetRect(rp, x, y, x + width, y + height);}/* * Functions: Radians, Degrees * Usage: radians = Radians(degrees); *        degrees = Degrees(radians); * ---------------------------------- * These functions convert back and forth between degrees and radians. */static double Radians(double degrees){    return (degrees * Pi / 180);}static double Degrees(double radians){    return (radians * 180 / Pi);}/* * Functions: InchesX, InchesY * Usage: inches = InchesX(pixels); *        inches = InchesY(pixels); * -------------------------------- * These functions convert distances measured in pixels to inches. * Because the resolution may not be uniform in the horizontal and * vertical directions, the coordinates are treated separately. */static double InchesX(int x){    return ((double) x / xResolution);}static double InchesY(int y){    return ((double) y / yResolution);}/* * Functions: PixelsX, PixelsY * Usage: pixels = PixelsX(inches); *        pixels = PixelsY(inches); * -------------------------------- * These functions convert distances measured in inches to pixels. */static int PixelsX(double x){    return (Round(x * xResolution + Epsilon));}static int PixelsY(double y){    return (Round(y * yResolution + Epsilon));}/* * Functions: ScaleX, ScaleY * Usage: pixels = ScaleX(inches); *        pixels = ScaleY(inches); * -------------------------------- * These functions are like PixelsX and PixelsY but convert coordinates * rather than lengths.  The difference is that y-coordinate values must * be inverted top to bottom to support the cartesian coordinates of * the graphics.h model. */static int ScaleX(double x){    return (PixelsX(x));}static int ScaleY(double y){    return (Round(PixelsY(windowHeight - y)));}static int Round(double x){    return ((int) floor(x + 0.5));}/* * Functions: Min, Max * Usage: min = Min(x, y); *        max = Max(x, y); * ----------------------- * These functions find the minimum and maximum of two integers. */static int Min(int x, int y){    return ((x < y) ? x : y);}static int Max(int x, int y){    return ((x > y) ? x : y);}/* * Functions: MinF, MaxF * Usage: min = MinF(x, y); *        max = MaxF(x, y); * ------------------------ * These functions find the minimum and maximum of two doubles. */static double MinF(double x, double y){    return ((x < y) ? x : y);}static double MaxF(double x, double y){    return ((x > y) ? x : y);}

⌨️ 快捷键说明

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