SimpleCG绘图函数(3)--绘制矩形

news2024/9/28 23:30:59

        前面我们已经学习了点和线的绘制,本篇我们继续绘图函数学习----矩形的绘制,也就是长方形的绘制,并给出一个绘制楼房的例子演示矩形的应用。

所有绘制矩形相关函数如下:

//以下矩形左上角坐标(left, top),右下角坐标(right,bottom )
//以线条颜色绘制边框,以填充颜色填充内部
 
//画无填充矩形
void rectangle( int left, int top, int right, int bottom );
 
//画无边框填充矩形
void solidrectangle( int left, int top, int right, int bottom );
 
//画填充矩形
void fillrectangle( int left, int top, int right, int bottom );
 
//清空矩形,以背景色清空矩形
void clearrectangle( int left, int top, int right, int bottom );

可以看到有四个绘制函数,而且参数都是一致的,参数标明矩形左上角坐标,及右下角坐标 。

四个函数效果分别是:

1、画无填充矩形  rectangle
 
2、画无边框填充矩形 solidrectangle


3、画填充矩形 fillrectangle 
 
4、清空矩形,以背景色清空矩形 clearrectangle 

我们以后会看到所有针对类似矩形这种具有面积的图形,都分别实现四种绘制效果,分别是1、无填充--不带有前缀,2、无边框--solid开头,3、带边框及填充--fill开头,4、清空形状区域,也就是以背景色填充。

下面用一个随机城市建筑的绘制例程展示矩形的简单应用:

// Building.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "../import/include/CGBoard.h"
#include "math.h"

#ifdef _DEBUG
#pragma comment(lib,"../import/lib/SimpleCG_MDd.lib")
#else
#pragma comment(lib,"../import/lib/SimpleCG_MD.lib")
#endif

#define SUBCOLOR(x,y) ((x-y)<0?0:(x-y))
#define ADDCOLOR(x,y) ((x+y)>255?255:(x+y))
int g_nWidth = 600;		//画面宽度
int g_nHeight= 600;		//画面高度

//画建筑的窗户1
void DrawWindow1( int nX, int nY, int nWidth, int nHeight, int nWidthCnt, int nHeightCnt )
{
	int i;
	int j;
	if(nWidthCnt<=0||nHeightCnt<=0)
		return;
	int nWidthWindow = nWidth / nWidthCnt;
	int nHeightWindow = nHeight / nHeightCnt;
	setfillcolor(RGB(rand()%200,130,180));
	for( j=0; j<nHeightCnt; j++ )
		for( i=0; i<nWidthCnt; i++ )
		{
			//画填充矩形
			fillrectangle( nX+nWidthWindow*i, nY+nHeightWindow*j, nX+nWidthWindow*i+nWidthWindow-5, nY+nHeightWindow*j+nHeightWindow-3 );
		}
}
//画建筑的窗户2
void DrawWindow2( int nX, int nY, int nWidth, int nHeight, int nHeightCnt )
{
	int i;
	int j;
	if(nHeightCnt<=0)
		return;
	int nHeightWindow = nHeight / nHeightCnt;
	setfillcolor(RGB(rand()%255,rand()%255,rand()%255));
	//画填充矩形
	fillrectangle( nX, nY, nX+nWidth, nY+nHeight );
	setfillcolor(RGB(200+rand()%55,200+rand()%55,200+rand()%55));
	for( j=0; j<nHeightCnt; j++ )
	{
		//填充矩形画横边
		solidrectangle( nX+1, nY+nHeightWindow*j, nX+nWidth, nY+nHeightWindow*j+3 );
	}
	//填充矩形画竖边
	if(rand()%2)
	{
		solidrectangle( nX+nWidth/5, nY, nX+nWidth/5+10, nY+nHeight );
		solidrectangle( nX+nWidth*4/5, nY, nX+nWidth*4/5+10, nY+nHeight );
	}
}
//画建筑的窗户3
void DrawWindow3( int nX, int nY, int nWidth, int nHeight, COLORREF  nColor )
{
	int i;
	int j;
	int nWidthCnt = nWidth / 10;
	int nHeightCnt = nHeight / 10;
	int r=GetRValue(nColor);
	int g=GetGValue(nColor);
	int b=GetBValue(nColor);
	setfillcolor(RGB( SUBCOLOR(r,50),SUBCOLOR(g,50),SUBCOLOR(b,50) ));
	//画填充矩形
	solidrectangle( nX, nY, nX+nWidth, nY+nHeight );
	r=GetRValue(nColor);
	g=GetGValue(nColor);
	b=GetBValue(nColor);
	setfillcolor(RGB( ADDCOLOR(r,30),ADDCOLOR(g,30),ADDCOLOR(b,30) ));
	for( i=1; i<nWidthCnt; i++ )
	{
		//画填充矩形
		solidrectangle( nX+10*i, nY, nX+10*i+3, nY+nHeight );
	}
	for( i=1; i<nHeightCnt; i++ )
	{
		//画填充矩形
		solidrectangle( nX, nY+10*i, nX+nWidth, nY+10*i+3 );
	}	
}
//画建筑类型1
void DrawBuilding1( int nX, int nY, int nWidth, int nHeight )
{
	int nWidthCnt = (nWidth-10) / 20;
	int nHeightCnt = (nHeight-10) / 30;
	//画填充矩形
	COLORREF  nColor= RGB(rand()%255,rand()%255,rand()%255);
	if(rand()%2)
	{
		//房顶
		if(rand()%2)
		{
			setfillcolor(RGB(0,0,0));
			fillrectangle( nX + nWidth-nWidth/4, nY - nHeight-80, nX + nWidth-nWidth/4+3, nY - nHeight );
		}
		setfillcolor(nColor);
		if(rand()%2)
		{
			fillrectangle( nX + nWidth-nWidth/4-20, nY - nHeight-40, nX + nWidth-20-nWidth/4+20, nY - nHeight );
		}
		fillrectangle( nX+20, nY - nHeight-20, nX + nWidth-20, nY - nHeight );
	}
	setfillcolor(nColor);
	fillrectangle( nX, nY - nHeight, nX + nWidth, nY );
	int nWindowType=rand()%2;
	switch(nWindowType)
	{
	case 1:
		DrawWindow1( nX+(nWidth-nWidthCnt*20)/2, nY - nHeight+(nHeight-nHeightCnt*30)/2, nWidthCnt*20, nHeightCnt*30, nWidthCnt, nHeightCnt );
		break;
	default:
		nWidthCnt = (nWidth-10) / 20;
		nHeightCnt = (nHeight-10) / 10;
		DrawWindow2( nX+(nWidth-nWidthCnt*20)/2, nY - nHeight+(nHeight-nHeightCnt*10)/2, nWidthCnt*20, nHeightCnt*10, nHeightCnt );
		break;
	}
}
//画建筑类型2
void DrawBuilding2( int nX, int nY, int nWidth, int nHeight )
{
	int nFloorHeight=nHeight/3;
	int nFloorWidth=nWidth/3;
	int nWidthCnt = (nFloorWidth-10) / 20;
	int nHeightCnt = (nFloorHeight-6) / 30;
	COLORREF  nColor= RGB(rand()%255,rand()%255,rand()%255);
	
	//房顶
	if(rand()%2)
	{
		setfillcolor(RGB(0,0,0));
		fillrectangle( nX + nFloorWidth/4, nY - nHeight-80, nX + nFloorWidth/4+3, nY - nHeight );
	}
	//房体
	//画填充矩形
	setfillcolor(nColor);
	fillrectangle( nX, nY - nHeight, nX + nFloorWidth, nY - nHeight + nFloorHeight );
	DrawWindow1( nX+(nFloorWidth-nWidthCnt*20)/2, nY - nHeight+(nFloorHeight-nHeightCnt*30)/2, nWidthCnt*20, nHeightCnt*30, nWidthCnt, nHeightCnt );
	fillrectangle( nX, nY - nHeight + nFloorHeight, nX + nFloorWidth*2, nY - nHeight + nFloorHeight*2 );
	DrawWindow1( nX+(nFloorWidth-nWidthCnt*20)/2, nY - nHeight+nFloorHeight+(nFloorHeight-nHeightCnt*30)/2, nWidthCnt*40, nHeightCnt*30, nWidthCnt*2, nHeightCnt );
	fillrectangle( nX, nY - nHeight + nFloorHeight*2, nX + nWidth, nY );
	DrawWindow1( nX+(nFloorWidth-nWidthCnt*20)/2, nY - nHeight+nFloorHeight*2+(nFloorHeight-nHeightCnt*30)/2, nWidthCnt*60, nHeightCnt*30, nWidthCnt*3, nHeightCnt );
}
void DrawBuilding3Floor( int nX, int nY, int nWidth, int nHeight, COLORREF  nColor )
{
	//画填充矩形
	int nPad=5;
	setfillcolor(nColor);
	fillrectangle( nX, nY, nX + nWidth, nY + nHeight );
	DrawWindow3( nX+nPad, nY +nPad, nWidth-nPad*2, nHeight  - nPad*2, nColor );
}
//画建筑类型3
void DrawBuilding3( int nX, int nY, int nWidth, int nHeight )
{
	int i;
	int nPad=3;
	int nInner=10;
	int nWindowWidth=10;
	int nFloorHeight=nHeight/8;
	int nWidthCnt = (nWidth-30) / nWindowWidth;
	int nHeightCnt = (nFloorHeight-10) / nWindowWidth;
	COLORREF  nColor= RGB(rand()%200,rand()%200,rand()%200);

	int y=nY;
	for( i=0; i<8; i++ )
	{
		if(i%2==0)
			nInner = 10;
		else
			nInner = 0;
		DrawBuilding3Floor( nX+nInner, y-nFloorHeight, nWidth, nFloorHeight, nColor);
		y -= nFloorHeight;
	}
}
void DrawProcess()
{
	int i;
	line( 0, g_nHeight-80, g_nWidth, g_nHeight-80 );
	
	for( i=0; i<8; i++ )
	{
		int nWindowType=rand()%3;
		switch(nWindowType)
		{
		case 1:
			DrawBuilding1( rand()%500, g_nHeight-80, 100+rand()%200, 220+rand()%200 );
			break;
		case 2:
			DrawBuilding2( rand()%500, g_nHeight-80, 100+rand()%200, 220+rand()%200 );
			break;
		default:
			DrawBuilding3( rand()%500, g_nHeight-80, 100+rand()%200, 220+rand()%200 );
			break;
		}
	}
	//马路
	setfillcolor(0);
	fillrectangle( 0, g_nHeight-80, g_nWidth, g_nHeight );
	setfillcolor(RGB(255,255,2555));
	for( i=0; i<g_nWidth; i+=100 )
	{
		solidrectangle( i, g_nHeight-60, i+50, g_nHeight-50 );
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	srand(GetTickCount());
	//初始化
	if( !ShowingBoard(g_nWidth,g_nHeight, DrawProcess))
		return 1;
	//关闭图库
	CloseBoard();
	return 0;
}

可以看到所有图形元素都是用矩形绘制,发挥你的想象,你一定能发现矩形的更多更好玩的用法。

运行界面:

 每次运行都随机生成各种建筑,我只构建了简单的三种建筑类型,你可以尝试修改代码,绘制更多更有趣的建筑。赶紧动起手来吧。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/586707.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

跨境电商系统开发-电商商城系统平台定制方案

随着业务的拓展&#xff0c;不少企业开始将目光转向国外市场&#xff0c;那么如何定制一套属于想自己的跨境出海电商商城方案呢&#xff1f;需要做好以下关口把关&#xff1a; 欢迎名片交流探讨开发平台流程 买家端(h5/pc/app) www.mardao.cn 账号 密码 卖家端(h5/pc)…

八、(重点)视图集ModelViewSet自定义action路由routers

上一章&#xff1a; 七、Django DRF框架GenericAPIView--搜索&排序&分页&返回值_做测试的喵酱的博客-CSDN博客 下一章&#xff1a; 九、DRF生成API文档_做测试的喵酱的博客-CSDN博客 一、视图集ModelViewSet与ReadOnlyViesSet ModelViewSet视图集 与 ReadOnly…

基于FPGA:运动目标检测(包围盒仿真工程,及一些调试问题)

目录 前言一、安装器件库二、仿真工程操作1、进入文件列表2、找到bounding_box_locate.vt&#xff0c;双击打开文件3、修改路径4、路径设置5、切换回“Hierarchy”&#xff0c;即工程界面6、运行仿真7、查看波形 重点&#xff1a;调试问题三、仿真代码1、仿真顶层文件2、绘制包…

node篇-fs模块儿

nodejs-fs模儿 异步 1. mkdir() 创建一个目录 // 1.mkdir 创建一个目录&#xff0c;回调函数的参数含义&#xff1a;err const fs require(fs); fs.mkdir(./avater,(err)>{console.log(err);if(err && err.code EEXIST){console.log(当前目录已经存在)} }) 当我…

华硕天选4R FA617原装Windows11原厂预装系统工厂模式恢复安装带 ASUSRecevory 一键还原22H2版本

华硕天选4R FA617X原装Windows11原厂预装系统工厂模式恢复安装带ASUSRecevory一键还原 文件地址&#xff1a;https://pan.baidu.com/s/1Pq09oDzmFI6hXVdf8Vqjqw?pwd3fs8 提取码:3fs8 华硕工厂恢复系统 &#xff0c;安装结束后带隐藏分区以及机器所有驱动软件 需准备一个16…

浅谈NoSQL数据库

数据库 数据库&#xff0c;又称为数据管理系统&#xff0c;是处理的数据按照一定的方式储存在一起&#xff0c;能够让多个用户共享、尽可能减小冗余度的数据集合&#xff0c;简而言之可视为电子化的文件柜——存储电子文件的处所。 数据库有&#xff1a;Oracle数据库、ACCESS数…

代码随想录算法训练营第四十五天 | 力扣 70. 爬楼梯(进阶), 322. 零钱兑换, 279.完全平方数

70. 爬楼梯&#xff08;进阶&#xff09; 题目 70. 爬楼梯 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢&#xff1f; 改为&#xff1a;一步一个台阶&#xff0c;两个台阶&#xff0c;三个台阶&#xff…

【浅学 JDBC】

浅学JDBC 笔记记录 一、1. JDBC的概念2. JDBC快速入门2.1 DriverManager2.2 Connection2.3 Statement2.4 ResultSet 3. JDBC入门案例使用3.1 查询所有学生信息3.2 根据id查询学生信息&&新增学生信息&&修改学生信息&&删除学生信息 一、 1. JDBC的概念 …

中科易安8周年,与你相约联网智能门锁

中科易安与物联网技术发展同频 持续推动安防信息化建设 打造多场景应用的数智化通行解决方案 促进技术与安全精准对接 联网智能门锁技术硬核 中科易安打造集NB-IoT、Sub-1G Cat.1、Wifi、RS485和BLE 5.0 在内的六大通信技术组网方案 以“联网”赋能智能门锁 实现通行数…

C++11之atomic原子操作

atomic介绍 多线程间是通过互斥锁与条件变量来保证共享数据的同步的&#xff0c;互斥锁主要是针对过程加锁来实现对共享资源的排他性访问。很多时候&#xff0c;对共享资源的访问主要是对某一数据结构的读写操作&#xff0c;如果数据结构本身就带有排他性访问的特性&#xff0c…

chatgpt赋能python:Python中的等待:理解和优化

Python中的等待&#xff1a;理解和优化 Python是一种强大的编程语言&#xff0c;在构建各种应用程序时很常用。但是&#xff0c;随着应用程序越来越复杂&#xff0c;需要等待一些操作时&#xff0c;Python中的等待传统上会导致性能下降。在这篇文章中&#xff0c;我们将深入了…

可持续能源技术改变世界

文章目录 一、你在工作或生活中接触过可持续能源技术吗&#xff1f;可以分享下你的经历与看法。二、你认为可持续能源技术的优势和挑战有哪些&#xff1f;三、你了解过可持续能源技术的应用现状吗&#xff1f;四、对于可持续能源技术真的否改变世界这个问题你怎么看&#xff1f…

ifconfig工具与驱动交互解析(ioctl)

Linux ifconfig&#xff08;network interfaces configuring&#xff09; Linux ifconfig命令用于显示或设置网络设备。ifconfig可设置网络设备的状态&#xff0c;或是显示目前的设置。同netstat一样&#xff0c;ifconfig源码也位于net-tools中。源码位于net-tools工具包中&am…

《消息队列高手课》课程笔记(七)

如何使用异步设计提升系统性能&#xff1f; 异步设计如何提升系统性能&#xff1f; 假设我们要实现一个转账的微服务 Transfer(accountFrom, accountTo, amount)&#xff0c;这个服务有三个参数&#xff1a;分别是转出账户、转入账户和转账金额。 这个例子的实现过程中&…

chatgpt赋能python:Python中如何反转字符串:三种简单方法

Python中如何反转字符串&#xff1a;三种简单方法 当我们在处理字符串时&#xff0c;有时需要将其反向排列。在Python中&#xff0c;这可以通过以下三种简单方法实现&#xff1a; 1. 使用内置的切片方法 在Python中&#xff0c;可以使用字符串的切片方法将其反转。这种方法非…

(浙大陈越版)数据结构 第三章 树(上) 3.4 小白专场:树的同构(PTA编程题讲解)

题意理解和二叉树表示 给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换变成T2&#xff0c;则称两棵树是“同构”的。 eg1&#xff1a;现请你判断如下两棵树&#xff08;左侧为T1&#xff0c;右侧为T2&#xff09;是否为同构树&#xff1f; 显然T1可以通过有限次左右孩子…

REST风格 -- SpringMVC入门保姆级教程(四)

文章目录 前言四、REST风格1.了解REST风格2.REST风格写法一般步骤3.REST风格快速开发4.REST风格中的注解5. 案例&#xff1a;基于REST风格页面数据交互 总结 前言 为了巩固所学的知识&#xff0c;作者尝试着开始发布一些学习笔记类的博客&#xff0c;方便日后回顾。当然&#…

Redis高级篇 - 分布式缓存

分布式缓存 基于Redis集群解决单机Redis存在的问题 单机的Redis存在四大问题&#xff1a; 1.Redis持久化 Redis有两种持久化方案&#xff1a; RDB持久化AOF持久化 1.1.RDB持久化 RDB全称Redis Database Backup file&#xff08;Redis数据备份文件&#xff09;&#xff0c…

iPad触屏笔哪个牌子好用?Apple Pencil的平替笔

从无纸化的广泛使用&#xff0c;电容笔成为无纸化中不可替代的一部分。但由于原装电容笔的昂贵&#xff0c;市面上的电容笔品牌众多&#xff0c;不知如何下手&#xff0c;今天给大家推荐几款好用又平价的Apple Pencil平替笔。顺便给不知道如何挑选电容笔的小伙伴科普一下电容笔…

车载网络测试 - CANCANFD - 基础篇_02

目录 七、与CAN总线相关的标准 1、ISO 11898,ISO16845 2、SAE J1939,ISO 11783,NMEA 2000,CANopen 3、ISO15765/ISO14229 4、ISO 17356/OSEK 5、CCP(CAN Calibration Protocol) 6、GMLAN,VWTP,FNOS,DCNet,MCNet 八、CAN总线的特点 九、CAN总线基本概念 七、与CAN总线相…