首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C >> C 語言實例練習

C語言練習實例65:TC圖形庫繪製優美圖案(LineTo演示)

     編輯:C 語言實例練習

C語言練習實例65:TC圖形庫繪製優美圖案(LineTo演示)

本實例使用 Turbo C 的 graphics.h 庫,在圓周上取等分點,兩兩連線繪製正多邊形內部所有對角線,形成優美幾何圖案。

程序分析

  • 初始化圖形模式(CGA, CGAC0)。
  • 計算圓心、半徑,按角度步長(360/MAXPTS)計算圓上各點座標。
  • 繪製外接圓,雙重循環連接所有點對(moveto/lineto)。
  • 設置顏色、背景色增強視覺效果。

源代碼

// Created by www.runoob.com on 15/11/9.
// Copyright © 2015年 菜鳥教程. All rights reserved.
//
#include "graphics.h"
#include "math.h"
#include "dos.h"
#include "conio.h"
#include "stdlib.h"
#include "stdio.h"
#include "stdarg.h"

#define MAXPTS 15
#define PI 3.1415926

struct PTS { int x, y; };
double AspectRatio = 0.85;

void LineToDemo(void) {
    struct viewporttype vp;
    struct PTS points[MAXPTS];
    int i, j, h, w, xcenter, ycenter;
    int radius, angle, step;
    double rads;

    getviewsettings(&vp);
    h = vp.bottom - vp.top;
    w = vp.right - vp.left;
    xcenter = w / 2;
    ycenter = h / 2;
    radius = (h - 30) / (AspectRatio * 2);
    step = 360 / MAXPTS;
    angle = 0;

    for (i = 0; i < MAXPTS; ++i) {
        rads = (double)angle * PI / 180.0;
        points[i].x = xcenter + (int)(cos(rads) * radius);
        points[i].y = ycenter - (int)(sin(rads) * radius * AspectRatio);
        angle += step;
    }

    circle(xcenter, ycenter, radius);
    for (i = 0; i < MAXPTS; ++i)
        for (j = i; j < MAXPTS; ++j) {
            moveto(points[i].x, points[i].y);
            lineto(points[j].x, points[j].y);
        }
}

int main() {
    int driver = CGA, mode = CGAC0;
    initgraph(&driver, &mode, "");
    setcolor(3);
    setbkcolor(GREEN);
    LineToDemo();
    return 0;
}

運行環境

僅能在 Turbo C / DOSBox 等 16 位實模式環境下運行,現代 IDE(VS Code, Visual Studio, GCC)無法直接編譯運行,需移植至 SDL、OpenGL 或現代圖形庫。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved