控制台上画三角形圆和直线

基于C语言的。

#include<stdio.h>
#include<stdlib.h>
#include<tchar.h>
#include<windows.h>
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow ();//把控制台的句柄给hwnd
int main()
{
     HWND hwnd;
     HDC hdc;
     HFONT font;
     HPEN hPen;
     HBRUSH hBrush;

     char str[]="王旭";
     hwnd = GetConsoleWindow();
     hdc = GetDC(hwnd);

     //system("color 3d");
     font=CreateFont(70,70,0,0,FW_BOLD,TRUE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
           CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,TEXT("微软雅黑")); //设置字体样式
     hPen=CreatePen(PS_INSIDEFRAME,0,RGB(255,0,0)); //设置画笔颜色
     hBrush=CreateSolidBrush(RGB(0,255,0));//设置画刷颜色
     SelectObject(hdc,hPen); 
     SelectObject(hdc,font); 
     //SelectObject(hdc,hBrush);不用选择填充
     SetBkColor(hdc,RGB(200,100,180)); //改变文本框背景色
     SetTextColor(hdc,RGB(100,200,150)); //改变文本框中字体颜色
     Rectangle(hdc,200,100,400,280);
     //画三角形   
     MoveToEx(hdc,50,50,NULL);
     LineTo(hdc,100,100);
     LineTo(hdc,150,50);
     LineTo(hdc,50,50);
     //画直线
     MoveToEx(hdc,200,50,NULL);
     LineTo(hdc,300,50);
     //画圆
     Ellipse(hdc,10,100,110,200);

     TextOut(hdc,50,300,str,sizeof(str));
     //删除部分
     DeleteObject(hPen);
     DeleteObject(font);
     DeleteObject(hBrush);
     ReleaseDC(hwnd,hdc);
     getchar();
     return 0;
}
下面是一个简单的 C++ 控制台应用程序,用于绘制三角形直线和矩形: ```c++ #include <iostream> #include <cmath> using namespace std; void drawLine(int x1, int y1, int x2, int y2) { int dx = abs(x2 - x1), dy = abs(y2 - y1); int sx = x1 < x2 ? 1 : -1, sy = y1 < y2 ? 1 : -1; int err = dx - dy; while (x1 != x2 || y1 != y2) { cout << "."; int e2 = 2 * err; if (e2 > -dy) { err -= dy; x1 += sx; } if (e2 < dx) { err += dx; y1 += sy; } } cout << endl; } void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) { drawLine(x1, y1, x2, y2); drawLine(x2, y2, x3, y3); drawLine(x3, y3, x1, y1); } void drawCircle(int xc, int yc, int r) { int x = 0, y = r; int d = 3 - 2 * r; while (x <= y) { cout << "."; if (d < 0) { d += 4 * x + 6; } else { d += 4 * (x - y) + 10; y--; } x++; } cout << endl; } void drawRectangle(int x, int y, int w, int h) { drawLine(x, y, x + w, y); drawLine(x + w, y, x + w, y + h); drawLine(x + w, y + h, x, y + h); drawLine(x, y + h, x, y); } int main() { // 绘制三角形 cout << "Triangle:" << endl; drawTriangle(10, 10, 20, 30, 30, 10); // 绘制形 cout << "Circle:" << endl; drawCircle(20, 20, 10); // 绘制直线 cout << "Line:" << endl; drawLine(10, 30, 30, 10); // 绘制矩形 cout << "Rectangle:" << endl; drawRectangle(40, 10, 20, 30); return 0; } ``` 这个程序使用了一些基本的绘图算法,包括绘制直线的 Bresenham 算法、绘制形的中点算法、以及绘制矩形的基于直线的算法。你可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值