前面我们已经学习了点和线的绘制,本篇我们继续绘图函数学习----矩形的绘制,也就是长方形的绘制,并给出一个绘制楼房的例子演示矩形的应用。
所有绘制矩形相关函数如下:
//以下矩形左上角坐标(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;
}
可以看到所有图形元素都是用矩形绘制,发挥你的想象,你一定能发现矩形的更多更好玩的用法。
运行界面:
每次运行都随机生成各种建筑,我只构建了简单的三种建筑类型,你可以尝试修改代码,绘制更多更有趣的建筑。赶紧动起手来吧。