当前位置: 首页 > 工具软件 > easy-browser > 使用案例 >

easyx

仲君昊
2023-12-01

EasyX简易绘图实例

EasyX简易绘图实例

绘制一个圆从左向右移动

<span style="font-size:18px;"><span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <graphics.h></span>
<span style="FONT-WEIGHT: bold" class="keyword">int</span> main()
{
<span class="indent">  </span>initgraph(<span class="number" style="color:#09999;">640</span>,<span class="number" style="color:#09999;">480</span>);<span class="comment" style="color:#999988;FONT-STYLE: italic">//绘图环境640*480</span>
<span class="indent">  </span>setcolor(WHITE);<span class="comment" style="color:#999988;FONT-STYLE: italic">//绘图前景色为白色</span>
<span class="indent">  </span>setfillstyle(BS_SOLID);<span class="comment" style="color:#999988;FONT-STYLE: italic">//填充样式为固实填充</span>
<span class="indent">  </span>setfillcolor(RED);<span class="comment" style="color:#999988;FONT-STYLE: italic">//填充颜色为蓝色</span>
<span class="indent">  </span>BeginBatchDraw();<span class="comment" style="color:#999988;FONT-STYLE: italic">//开始批量绘图</span>
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">for</span>(<span style="FONT-WEIGHT: bold" class="keyword">int</span> i=<span class="number" style="color:#09999;">50</span>; i<<span class="number" style="color:#09999;">600</span>; i++)
<span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span>circle(i,<span class="number" style="color:#09999;">100</span>,<span class="number" style="color:#09999;">40</span>);<span class="comment" style="color:#999988;FONT-STYLE: italic">//画圆</span>
<span class="indent">  </span><span class="indent">  </span>floodfill(i, <span class="number" style="color:#09999;">100</span>, WHITE);<span class="comment" style="color:#999988;FONT-STYLE: italic">//填充</span>
<span class="indent">  </span><span class="indent">  </span>FlushBatchDraw();<span class="comment" style="color:#999988;FONT-STYLE: italic">//执行未完成的绘制任务</span>
<span class="indent">  </span><span class="indent">  </span>Sleep(<span class="number" style="color:#09999;">10</span>);<span class="comment" style="color:#999988;FONT-STYLE: italic">//挂起50毫秒</span>
<span class="indent">  </span><span class="indent">  </span>cleardevice();<span class="comment" style="color:#999988;FONT-STYLE: italic">//用背景色清空屏幕</span>
<span class="indent">  </span>}
<span class="indent">  </span>EndBatchDraw();<span class="comment" style="color:#999988;FONT-STYLE: italic">//结束批量绘制</span>
<span class="indent">  </span>closegraph();<span class="comment" style="color:#999988;FONT-STYLE: italic">//关闭图形环境</span>
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">return</span> <span class="number" style="color:#09999;">0</span>;
}</span>

力学:弹跳球模拟程序

<span style="font-size:18px;"><span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <graphics.h></span>
<span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <conio.h></span>
<span style="FONT-WEIGHT: bold" class="keyword">int</span> main()
{
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">double</span> h = <span class="number" style="color:#09999;">300</span>;				<span class="comment" style="color:#999988;FONT-STYLE: italic">// 高度</span>
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">double</span> v = <span class="number" style="color:#09999;">0</span>;				<span class="comment" style="color:#999988;FONT-STYLE: italic">// 速度(方向向下)</span>
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">double</span> dv = <span class="number" style="color:#09999;">9.8</span> / <span class="number" style="color:#09999;">50</span>;		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 加速度(每 1/50 秒)</span>
<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 初始化绘图窗口</span>
<span class="indent">  </span>initgraph(<span class="number" style="color:#09999;">640</span>, <span class="number" style="color:#09999;">480</span>);
<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 画地平线</span>
<span class="indent">  </span>line(<span class="number" style="color:#09999;">100</span>, <span class="number" style="color:#09999;">421</span>, <span class="number" style="color:#09999;">540</span>, <span class="number" style="color:#09999;">421</span>);
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">while</span>(!_kbhit())
<span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span>v += dv;				<span class="comment" style="color:#999988;FONT-STYLE: italic">// 根据加速度计算速度</span>
<span class="indent">  </span><span class="indent">  </span>h -= (v - dv / <span class="number" style="color:#09999;">2</span>);		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 计算高度</span>
<span class="indent">  </span><span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 如果高度低于地平线,实现反弹,速度方向取反</span>
<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">if</span> (h <= <span class="number" style="color:#09999;">0</span>)
<span class="indent">  </span><span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>h += (v - dv / <span class="number" style="color:#09999;">2</span>);
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>v = - v * <span class="number" style="color:#09999;">0.9</span>;		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 反弹时能量损耗 10%</span>
<span class="indent">  </span><span class="indent">  </span>}
<span class="indent">  </span><span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 画绿色球</span>
<span class="indent">  </span><span class="indent">  </span>setcolor(GREEN);
<span class="indent">  </span><span class="indent">  </span>circle(<span class="number" style="color:#09999;">320</span>, <span class="number" style="color:#09999;">400</span> - <span style="FONT-WEIGHT: bold" class="keyword">int</span>(h), <span class="number" style="color:#09999;">20</span>);
<span class="indent">  </span><span class="indent">  </span>Sleep(<span class="number" style="color:#09999;">20</span>);		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 延时(每帧延时 1/50 秒)</span>
<span class="indent">  </span><span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 擦掉球</span>
<span class="indent">  </span><span class="indent">  </span>setcolor(BLACK);
<span class="indent">  </span><span class="indent">  </span>circle(<span class="number" style="color:#09999;">320</span>, <span class="number" style="color:#09999;">400</span> - <span style="FONT-WEIGHT: bold" class="keyword">int</span>(h), <span class="number" style="color:#09999;">20</span>);
<span class="indent">  </span>}
<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 关闭绘图窗口</span>
<span class="indent">  </span>closegraph();
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">return</span> <span class="number" style="color:#09999;">0</span>;
}</span>

钟表模拟程序(表针形式)

<span style="font-size:18px;"><span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <graphics.h></span>
<span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <conio.h></span>
<span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <math.h></span>

<span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#<span class="keyword" style="color:#333333;">define</span>	PI	3.1415926536</span>

<span style="FONT-WEIGHT: bold" class="keyword">void</span> DrawHand(<span style="FONT-WEIGHT: bold" class="keyword">int</span> hour, <span style="FONT-WEIGHT: bold" class="keyword">int</span> minute, <span style="FONT-WEIGHT: bold" class="keyword">int</span> second)
{
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">double</span> a_hour, a_min, a_sec;					<span class="comment" style="color:#999988;FONT-STYLE: italic">// 时、分、秒针的弧度值</span>
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">int</span> x_hour, y_hour, x_min, y_min, x_sec, y_sec;	<span class="comment" style="color:#999988;FONT-STYLE: italic">// 时、分、秒针的末端位置</span>

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 计算时、分、秒针的弧度值</span>
<span class="indent">  </span>a_sec = second * <span class="number" style="color:#09999;">2</span> * PI / <span class="number" style="color:#09999;">60</span>;
<span class="indent">  </span>a_min = minute * <span class="number" style="color:#09999;">2</span> * PI / <span class="number" style="color:#09999;">60</span> + a_sec / <span class="number" style="color:#09999;">60</span>;
<span class="indent">  </span>a_hour= hour * <span class="number" style="color:#09999;">2</span> * PI / <span class="number" style="color:#09999;">12</span> + a_min / <span class="number" style="color:#09999;">12</span>;

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 计算时、分、秒针的末端位置</span>
<span class="indent">  </span>x_sec = <span style="FONT-WEIGHT: bold" class="keyword">int</span>(<span class="number" style="color:#09999;">120</span> * sin(a_sec));
<span class="indent">  </span>y_sec = <span style="FONT-WEIGHT: bold" class="keyword">int</span>(<span class="number" style="color:#09999;">120</span> * cos(a_sec));
<span class="indent">  </span>x_min = <span style="FONT-WEIGHT: bold" class="keyword">int</span>(<span class="number" style="color:#09999;">100</span> * sin(a_min));
<span class="indent">  </span>y_min = <span style="FONT-WEIGHT: bold" class="keyword">int</span>(<span class="number" style="color:#09999;">100</span> * cos(a_min));
<span class="indent">  </span>x_hour= <span style="FONT-WEIGHT: bold" class="keyword">int</span>(<span class="number" style="color:#09999;">70</span> * sin(a_hour));
<span class="indent">  </span>y_hour= <span style="FONT-WEIGHT: bold" class="keyword">int</span>(<span class="number" style="color:#09999;">70</span> * cos(a_hour));

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 画时针</span>
<span class="indent">  </span>setlinestyle(PS_SOLID, <span class="number" style="color:#09999;">10</span>);
<span class="indent">  </span>setcolor(WHITE);
<span class="indent">  </span>line(<span class="number" style="color:#09999;">320</span> + x_hour, <span class="number" style="color:#09999;">240</span> - y_hour, <span class="number" style="color:#09999;">320</span> - x_hour / <span class="number" style="color:#09999;">7</span>, <span class="number" style="color:#09999;">240</span> + y_hour / <span class="number" style="color:#09999;">7</span>);

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 画分针</span>
<span class="indent">  </span>setlinestyle(PS_SOLID, <span class="number" style="color:#09999;">6</span>);
<span class="indent">  </span>setcolor(LIGHTGRAY);
<span class="indent">  </span>line(<span class="number" style="color:#09999;">320</span> + x_min, <span class="number" style="color:#09999;">240</span> - y_min, <span class="number" style="color:#09999;">320</span> - x_min / <span class="number" style="color:#09999;">5</span>, <span class="number" style="color:#09999;">240</span> + y_min / <span class="number" style="color:#09999;">5</span>);

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 画秒针</span>
<span class="indent">  </span>setlinestyle(PS_SOLID, <span class="number" style="color:#09999;">2</span>);
<span class="indent">  </span>setcolor(RED);
<span class="indent">  </span>line(<span class="number" style="color:#09999;">320</span> + x_sec, <span class="number" style="color:#09999;">240</span> - y_sec, <span class="number" style="color:#09999;">320</span> - x_sec / <span class="number" style="color:#09999;">3</span>, <span class="number" style="color:#09999;">240</span> + y_sec / <span class="number" style="color:#09999;">3</span>);
}

<span style="FONT-WEIGHT: bold" class="keyword">void</span> DrawDial()
{
<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 绘制一个简单的表盘</span>
<span class="indent">  </span>circle(<span class="number" style="color:#09999;">320</span>, <span class="number" style="color:#09999;">240</span>, <span class="number" style="color:#09999;">2</span>);
<span class="indent">  </span>circle(<span class="number" style="color:#09999;">320</span>, <span class="number" style="color:#09999;">240</span>, <span class="number" style="color:#09999;">60</span>);
<span class="indent">  </span>circle(<span class="number" style="color:#09999;">320</span>, <span class="number" style="color:#09999;">240</span>, <span class="number" style="color:#09999;">160</span>);
<span class="indent">  </span>outtextxy(<span class="number" style="color:#09999;">296</span>, <span class="number" style="color:#09999;">310</span>, <span class="string" style="color:#dd1144;">"BestAns"</span>);

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 绘制刻度</span>
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">int</span> x, y;
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">for</span> (<span style="FONT-WEIGHT: bold" class="keyword">int</span> i=<span class="number" style="color:#09999;">0</span>; i<<span class="number" style="color:#09999;">60</span>; i++)
<span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span>x = <span class="number" style="color:#09999;">320</span> + <span style="FONT-WEIGHT: bold" class="keyword">int</span>(<span class="number" style="color:#09999;">145</span> * sin(PI * <span class="number" style="color:#09999;">2</span> * i / <span class="number" style="color:#09999;">60</span>));
<span class="indent">  </span><span class="indent">  </span>y = <span class="number" style="color:#09999;">240</span> + <span style="FONT-WEIGHT: bold" class="keyword">int</span>(<span class="number" style="color:#09999;">145</span> * cos(PI * <span class="number" style="color:#09999;">2</span> * i / <span class="number" style="color:#09999;">60</span>));

<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">if</span> (i % <span class="number" style="color:#09999;">15</span> == <span class="number" style="color:#09999;">0</span>)
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>bar(x - <span class="number" style="color:#09999;">5</span>, y - <span class="number" style="color:#09999;">5</span>, x + <span class="number" style="color:#09999;">5</span>, y + <span class="number" style="color:#09999;">5</span>);
<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">else</span> <span style="FONT-WEIGHT: bold" class="keyword">if</span> (i % <span class="number" style="color:#09999;">5</span> == <span class="number" style="color:#09999;">0</span>)
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>circle(x, y, <span class="number" style="color:#09999;">3</span>);
<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">else</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>putpixel(x, y, WHITE);
<span class="indent">  </span>}
}

<span style="FONT-WEIGHT: bold" class="keyword">void</span> main()
{
<span class="indent">  </span>initgraph(<span class="number" style="color:#09999;">640</span>, <span class="number" style="color:#09999;">480</span>);		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 初始化 640 x 480 的绘图窗口</span>

<span class="indent">  </span>DrawDial();					<span class="comment" style="color:#999988;FONT-STYLE: italic">// 绘制表盘</span>

<span class="indent">  </span>setwritemode(R2_XORPEN);	<span class="comment" style="color:#999988;FONT-STYLE: italic">// 设置 XOR 绘图模式</span>

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 绘制表针</span>
<span class="indent">  </span>SYSTEMTIME ti;				<span class="comment" style="color:#999988;FONT-STYLE: italic">// 定义变量保存当前时间</span>
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">while</span>(!kbhit())				<span class="comment" style="color:#999988;FONT-STYLE: italic">// 按任意键退出钟表程序</span>
<span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span>GetLocalTime(&ti);		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 获取当前时间</span>
<span class="indent">  </span><span class="indent">  </span>DrawHand(ti.wHour, ti.wMinute, ti.wSecond);	<span class="comment" style="color:#999988;FONT-STYLE: italic">// 画表针</span>
<span class="indent">  </span><span class="indent">  </span>Sleep(<span class="number" style="color:#09999;">1000</span>);			<span class="comment" style="color:#999988;FONT-STYLE: italic">// 延时 1 秒</span>
<span class="indent">  </span><span class="indent">  </span>DrawHand(ti.wHour, ti.wMinute, ti.wSecond);	<span class="comment" style="color:#999988;FONT-STYLE: italic">// 擦表针(擦表针和画表针的过程是一样的)</span>
<span class="indent">  </span>}
<span class="indent">  </span>closegraph();				<span class="comment" style="color:#999988;FONT-STYLE: italic">// 关闭绘图窗口</span>
}</span>
3 辆行驶的小车
<span style="font-size:18px;"><span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <graphics.h></span>
<span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <conio.h></span>
<span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <stdlib.h></span>
<span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <stdio.h></span>

<span class="comment" style="color:#999988;FONT-STYLE: italic">// 声明需要使用的函数</span>
<span style="FONT-WEIGHT: bold" class="keyword">void</span> carstart(<span style="FONT-WEIGHT: bold" class="keyword">int</span> x, <span style="FONT-WEIGHT: bold" class="keyword">int</span> y, <span style="FONT-WEIGHT: bold" class="keyword">int</span> z);
<span style="FONT-WEIGHT: bold" class="keyword">void</span> drawbus();
<span style="FONT-WEIGHT: bold" class="keyword">void</span> init();

<span class="comment" style="color:#999988;FONT-STYLE: italic">// 定义全局变量</span>
IMAGE img;


<span class="comment" style="color:#999988;FONT-STYLE: italic"><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span>//</span>
<span style="FONT-WEIGHT: bold" class="keyword">void</span> main()
{
<span class="indent">  </span>init();
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">int</span> x=<span class="number" style="color:#09999;">0</span>, y=<span class="number" style="color:#09999;">0</span>, z=<span class="number" style="color:#09999;">0</span>;
<span class="indent">  </span>BeginBatchDraw();

<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">while</span>(!kbhit())
<span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span>x += <span class="number" style="color:#09999;">2</span>;
<span class="indent">  </span><span class="indent">  </span>y++;
<span class="indent">  </span><span class="indent">  </span>z += <span class="number" style="color:#09999;">3</span>;
<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">if</span> (x > <span class="number" style="color:#09999;">600</span>)	x = -<span class="number" style="color:#09999;">200</span>;
<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">if</span> (y > <span class="number" style="color:#09999;">600</span>)	y = -<span class="number" style="color:#09999;">200</span>;
<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">if</span> (z > <span class="number" style="color:#09999;">600</span>)	z = -<span class="number" style="color:#09999;">200</span>;

<span class="indent">  </span><span class="indent">  </span>carstart(x, y, z); 
<span class="indent">  </span><span class="indent">  </span>FlushBatchDraw(); 
<span class="indent">  </span><span class="indent">  </span>Sleep(<span class="number" style="color:#09999;">5</span>);
<span class="indent">  </span>}

<span class="indent">  </span>EndBatchDraw();
<span class="indent">  </span>closegraph();
}


<span class="comment" style="color:#999988;FONT-STYLE: italic"><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span>//</span>
<span class="comment" style="color:#999988;FONT-STYLE: italic">// 初始化函数,初始化窗口大小,获取所画图片</span>
<span style="FONT-WEIGHT: bold" class="keyword">void</span> init()
{
<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 初始化窗口大小</span>
<span class="indent">  </span>initgraph(<span class="number" style="color:#09999;">600</span>, <span class="number" style="color:#09999;">600</span>);

<span class="indent">  </span>outtextxy(<span class="number" style="color:#09999;">70</span>, <span class="number" style="color:#09999;">250</span>, <span class="string" style="color:#dd1144;">"大家好,新手来报到,希望大家多多指教"</span>); 
<span class="indent">  </span>outtextxy(<span class="number" style="color:#09999;">70</span>, <span class="number" style="color:#09999;">270</span>, <span class="string" style="color:#dd1144;">"下面你们会看到我程序的效果,程序很简单"</span>); 
<span class="indent">  </span>outtextxy(<span class="number" style="color:#09999;">70</span>, <span class="number" style="color:#09999;">290</span>, <span class="string" style="color:#dd1144;">"希望以后再跟大家的交流中学到更多,希望自己以后能编出更好的程序"</span>); 
<span class="indent">  </span>outtextxy(<span class="number" style="color:#09999;">70</span>, <span class="number" style="color:#09999;">320</span>, <span class="string" style="color:#dd1144;">"请按任意键进观看程序执行效果"</span>); 

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 等待按键按下</span>
<span class="indent">  </span>getch();

<span class="indent">  </span>cleardevice();						<span class="comment" style="color:#999988;FONT-STYLE: italic">// 清除上面的文字进入运行效果画面</span>
<span class="indent">  </span>drawbus();							<span class="comment" style="color:#999988;FONT-STYLE: italic">// 调用绘图函数,绘制 BUS</span>
<span class="indent">  </span>getimage(&img, <span class="number" style="color:#09999;">80</span>, <span class="number" style="color:#09999;">40</span>, <span class="number" style="color:#09999;">180</span>, <span class="number" style="color:#09999;">90</span>);	<span class="comment" style="color:#999988;FONT-STYLE: italic">// 获取 BUS 图片位置,保存在 img 变量中</span>
}


<span class="comment" style="color:#999988;FONT-STYLE: italic"><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span>//</span>
<span class="comment" style="color:#999988;FONT-STYLE: italic">// 车辆行驶程序,通过 putimge 函数,改变移动的像素来达到图片移动</span>
<span style="FONT-WEIGHT: bold" class="keyword">void</span> carstart(<span style="FONT-WEIGHT: bold" class="keyword">int</span> x, <span style="FONT-WEIGHT: bold" class="keyword">int</span> y, <span style="FONT-WEIGHT: bold" class="keyword">int</span> z)
{
<span class="indent">  </span>cleardevice();
<span class="indent">  </span>putimage(x, <span class="number" style="color:#09999;">40</span>, &img);
<span class="indent">  </span>setlinestyle(PS_SOLID, <span class="number" style="color:#09999;">10</span>);   <span class="comment" style="color:#999988;FONT-STYLE: italic">//设置画线的大小</span>
<span class="indent">  </span>line(<span class="number" style="color:#09999;">0</span>, <span class="number" style="color:#09999;">135</span>, <span class="number" style="color:#09999;">600</span>, <span class="number" style="color:#09999;">135</span>);
<span class="indent">  </span>putimage(y, <span class="number" style="color:#09999;">220</span>, &img);
<span class="indent">  </span>line(<span class="number" style="color:#09999;">0</span>, <span class="number" style="color:#09999;">315</span>, <span class="number" style="color:#09999;">600</span>, <span class="number" style="color:#09999;">315</span>);
<span class="indent">  </span>putimage(z, <span class="number" style="color:#09999;">380</span>, &img);
<span class="indent">  </span>line(<span class="number" style="color:#09999;">0</span>, <span class="number" style="color:#09999;">475</span>, <span class="number" style="color:#09999;">600</span>, <span class="number" style="color:#09999;">475</span>);
}


<span class="comment" style="color:#999988;FONT-STYLE: italic"><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span><span class="xmlDocTag">///</span>//</span>
<span class="comment" style="color:#999988;FONT-STYLE: italic">// 绘制 BUS 函数,通过画一些线条,组合它们的位置,组合成一辆小车</span>
<span style="FONT-WEIGHT: bold" class="keyword">void</span> drawbus()
{
<span class="indent">  </span>setcolor(RED);
<span class="indent">  </span>setfillstyle(BLUE);

<span class="indent">  </span>fillcircle(<span class="number" style="color:#09999;">120</span>, <span class="number" style="color:#09999;">120</span>, <span class="number" style="color:#09999;">10</span>); 		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车的轮胎</span>
<span class="indent">  </span>fillcircle(<span class="number" style="color:#09999;">200</span>, <span class="number" style="color:#09999;">120</span>, <span class="number" style="color:#09999;">10</span>); 		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车的轮胎</span>
<span class="indent">  </span>line(<span class="number" style="color:#09999;">80</span>,  <span class="number" style="color:#09999;">120</span>, <span class="number" style="color:#09999;">110</span>, <span class="number" style="color:#09999;">120</span>);		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车的底部车板</span>
<span class="indent">  </span>line(<span class="number" style="color:#09999;">80</span>,  <span class="number" style="color:#09999;">40</span>,  <span class="number" style="color:#09999;">80</span>,  <span class="number" style="color:#09999;">120</span>); 		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车的四周车板</span>
<span class="indent">  </span>line(<span class="number" style="color:#09999;">130</span>, <span class="number" style="color:#09999;">120</span>, <span class="number" style="color:#09999;">190</span>, <span class="number" style="color:#09999;">120</span>);		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车的底部车板</span>
<span class="indent">  </span>line(<span class="number" style="color:#09999;">210</span>, <span class="number" style="color:#09999;">120</span>, <span class="number" style="color:#09999;">250</span>, <span class="number" style="color:#09999;">120</span>);		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车的底部车板</span>
<span class="indent">  </span>line(<span class="number" style="color:#09999;">250</span>, <span class="number" style="color:#09999;">40</span>,  <span class="number" style="color:#09999;">250</span>, <span class="number" style="color:#09999;">120</span>); 		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车的四周车板</span>
<span class="indent">  </span>line(<span class="number" style="color:#09999;">80</span>,  <span class="number" style="color:#09999;">40</span>,  <span class="number" style="color:#09999;">250</span>, <span class="number" style="color:#09999;">40</span>);		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车的顶部车板</span>

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车窗</span>
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">for</span>(<span style="FONT-WEIGHT: bold" class="keyword">int</span> x = <span class="number" style="color:#09999;">90</span>, y = <span class="number" style="color:#09999;">100</span>; x < <span class="number" style="color:#09999;">190</span> && y < <span class="number" style="color:#09999;">190</span>; x += <span class="number" style="color:#09999;">15</span>, y += <span class="number" style="color:#09999;">15</span>)
<span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span>rectangle(x, <span class="number" style="color:#09999;">60</span>, y, <span class="number" style="color:#09999;">70</span>);
<span class="indent">  </span>}

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 画车门</span>
<span class="indent">  </span>rectangle(<span class="number" style="color:#09999;">220</span>, <span class="number" style="color:#09999;">60</span>, <span class="number" style="color:#09999;">240</span>, <span class="number" style="color:#09999;">120</span>);
<span class="indent">  </span>line(<span class="number" style="color:#09999;">230</span>, <span class="number" style="color:#09999;">60</span>, <span class="number" style="color:#09999;">230</span>, <span class="number" style="color:#09999;">120</span>);
<span class="indent">  </span>circle(<span class="number" style="color:#09999;">230</span>, <span class="number" style="color:#09999;">90</span>, <span class="number" style="color:#09999;">5</span>); 
}</span>
鼠标操作演示
<span style="font-size:18px;"><span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <graphics.h></span>
<span class="preprocessor" style="color:#999999;FONT-WEIGHT: bold">#include <conio.h></span>

<span style="FONT-WEIGHT: bold" class="keyword">int</span> main()
{
<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 初始化图形窗口</span>
<span class="indent">  </span>initgraph(<span class="number" style="color:#09999;">640</span>, <span class="number" style="color:#09999;">480</span>);

<span class="indent">  </span>MOUSEMSG m;		<span class="comment" style="color:#999988;FONT-STYLE: italic">// 定义鼠标消息</span>

<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">while</span>(<span style="FONT-WEIGHT: bold" class="keyword">true</span>)
<span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 获取一条鼠标消息</span>
<span class="indent">  </span><span class="indent">  </span>m = GetMouseMsg();

<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">switch</span>(m.uMsg)
<span class="indent">  </span><span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">case</span> WM_MOUSEMOVE:
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 鼠标移动的时候画红色的小点</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>putpixel(m.x, m.y, RED);
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">break</span>;

<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">case</span> WM_LBUTTONDOWN:
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 如果点左键的同时按下了 Ctrl 键</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">if</span> (m.mkCtrl)
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 画一个大方块</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>rectangle(m.x-<span class="number" style="color:#09999;">10</span>, m.y-<span class="number" style="color:#09999;">10</span>, m.x+<span class="number" style="color:#09999;">10</span>, m.y+<span class="number" style="color:#09999;">10</span>);
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">else</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 画一个小方块</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>rectangle(m.x-<span class="number" style="color:#09999;">5</span>, m.y-<span class="number" style="color:#09999;">5</span>, m.x+<span class="number" style="color:#09999;">5</span>, m.y+<span class="number" style="color:#09999;">5</span>);
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">break</span>;

<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">case</span> WM_RBUTTONUP:
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">return</span> <span class="number" style="color:#09999;">0</span>;	<span class="comment" style="color:#999988;FONT-STYLE: italic">// 按鼠标右键退出程序</span>
<span class="indent">  </span><span class="indent">  </span>}
<span class="indent">  </span>}

<span class="indent">  </span><span class="comment" style="color:#999988;FONT-STYLE: italic">// 关闭图形窗口</span>
<span class="indent">  </span>closegraph();
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">return</span> <span class="number" style="color:#09999;">0</span>
}</span>

 模拟蒙特卡罗法求π的值

<span style="font-size:18px;"><span class="comment" style="color:#999988;FONT-STYLE: italic">#include <graphics.h></span>
<span class="comment" style="color:#999988;FONT-STYLE: italic">#include <conio.h></span>
<span class="comment" style="color:#999988;FONT-STYLE: italic">#include <time.h></span>
<span class="comment" style="color:#999988;FONT-STYLE: italic">#include <stdlib.h></span>
<span class="comment" style="color:#999988;FONT-STYLE: italic">#include <stdio.h></span>
<span style="FONT-WEIGHT: bold" class="keyword">int</span> main()
{
<span class="indent">  </span>long <span style="FONT-WEIGHT: bold" class="keyword">m</span>=<span class="number" style="color:#09999;">0</span>,n=<span class="number" style="color:#09999;">0</span>,i;
<span class="indent">  </span>double xi,yi,<span style="FONT-WEIGHT: bold" class="keyword">y</span>;
<span class="indent">  </span>initgraph(<span class="number" style="color:#09999;">640</span>, <span class="number" style="color:#09999;">480</span>);<span class="regexp" style="color:#09926;">//</span> 初始化 <span class="number" style="color:#09999;">640</span> <span style="FONT-WEIGHT: bold" class="keyword">x</span> <span class="number" style="color:#09999;">480</span> 的绘图窗口
<span class="indent">  </span>setcolor(YELLOW);
<span class="regexp" style="color:#09926;">//line</span>(<span class="number" style="color:#09999;">200</span>,<span class="number" style="color:#09999;">50</span>,<span class="number" style="color:#09999;">200</span>,<span class="number" style="color:#09999;">400</span>);line(<span class="number" style="color:#09999;">200</span>,<span class="number" style="color:#09999;">50</span>,<span class="number" style="color:#09999;">205</span>,<span class="number" style="color:#09999;">60</span>); line(<span class="number" style="color:#09999;">200</span>,<span class="number" style="color:#09999;">50</span>,<span class="number" style="color:#09999;">195</span>,<span class="number" style="color:#09999;">60</span>);
<span class="regexp" style="color:#09926;">//line</span>(<span class="number" style="color:#09999;">50</span>,<span class="number" style="color:#09999;">200</span>,<span class="number" style="color:#09999;">400</span>,<span class="number" style="color:#09999;">200</span>);line(<span class="number" style="color:#09999;">400</span>,<span class="number" style="color:#09999;">200</span>,<span class="number" style="color:#09999;">390</span>,<span class="number" style="color:#09999;">195</span>); line(<span class="number" style="color:#09999;">400</span>,<span class="number" style="color:#09999;">200</span>,<span class="number" style="color:#09999;">390</span>,<span class="number" style="color:#09999;">205</span>);
<span class="indent">  </span>rectangle(<span class="number" style="color:#09999;">220</span>,<span class="number" style="color:#09999;">100</span>,<span class="number" style="color:#09999;">420</span>,<span class="number" style="color:#09999;">300</span>);
<span class="indent">  </span>circle(<span class="number" style="color:#09999;">320</span>,<span class="number" style="color:#09999;">200</span>,<span class="number" style="color:#09999;">100</span>);
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">srand</span>((unsigned)<span style="FONT-WEIGHT: bold" class="keyword">time</span>(NULL));
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">for</span>(i=<span class="number" style="color:#09999;">0</span>; i<<span class="number" style="color:#09999;">100000</span>; i++)   /<span class="variable" style="color:#08080;">*设</span>置产生随机数的个数<span class="variable" style="color:#08080;">*/</span>
<span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span><span class="regexp" style="color:#09926;">/*---产生-1到1之间的随机数xi和yi-----*/</span>
<span class="indent">  </span><span class="indent">  </span>xi=(<span style="FONT-WEIGHT: bold" class="keyword">rand</span>()<span class="variable" style="color:#08080;">%(</span><span class="number" style="color:#09999;">1000</span>-<span class="number" style="color:#09999;">0</span>+<span class="number" style="color:#09999;">1</span>)+<span class="number" style="color:#09999;">0</span>)/<span class="number" style="color:#09999;">500.0</span>-<span class="number" style="color:#09999;">1.0</span>;
<span class="indent">  </span><span class="indent">  </span>yi=(<span style="FONT-WEIGHT: bold" class="keyword">rand</span>()<span class="variable" style="color:#08080;">%(</span><span class="number" style="color:#09999;">1000</span>-<span class="number" style="color:#09999;">0</span>+<span class="number" style="color:#09999;">1</span>)+<span class="number" style="color:#09999;">0</span>)/<span class="number" style="color:#09999;">500.0</span>-<span class="number" style="color:#09999;">1.0</span>;
<span class="indent">  </span><span class="indent">  </span>putpixel((<span style="FONT-WEIGHT: bold" class="keyword">int</span>)(xi<span class="variable" style="color:#08080;">*100</span>+<span class="number" style="color:#09999;">320</span>),(<span style="FONT-WEIGHT: bold" class="keyword">int</span>)(yi<span class="variable" style="color:#08080;">*100</span>+<span class="number" style="color:#09999;">200</span>),(<span style="FONT-WEIGHT: bold" class="keyword">int</span>)GREEN);<span class="regexp" style="color:#09926;">/*在正方形区域内画点*/</span>
<span class="indent">  </span><span class="indent">  </span>/<span class="variable" style="color:#08080;">*printf</span>(<span class="string" style="color:#dd1144;">"xi=<span class="variable" style="color:#08080;">%f</span>,yi=<span class="variable" style="color:#08080;">%f</span>\n"</span>,xi,yi);<span class="variable" style="color:#08080;">*/</span>
<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">if</span>(yi<span class="variable" style="color:#08080;">*yi</span>>=(-(<span class="number" style="color:#09999;">1</span>-xi<span class="variable" style="color:#08080;">*xi</span>))&&yi<span class="variable" style="color:#08080;">*yi</span><=<span class="number" style="color:#09999;">1</span>-xi<span class="variable" style="color:#08080;">*xi</span>) /<span class="variable" style="color:#08080;">*判</span>断是否在圆内并计数<span class="variable" style="color:#08080;">*/</span>
<span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">m</span>++,n++;
<span class="indent">  </span><span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">else</span> <span style="FONT-WEIGHT: bold" class="keyword">m</span>++;
<span class="indent">  </span><span class="indent">  </span>Sleep(<span class="number" style="color:#09999;">10</span>);
<span class="indent">  </span>}
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">y</span>=<span class="number" style="color:#09999;">4.0</span><span class="variable" style="color:#08080;">*n</span>/<span style="FONT-WEIGHT: bold" class="keyword">m</span>;
<span class="indent">  </span><span class="regexp" style="color:#09926;">/*计算PI值*/</span>
<span class="indent">  </span>char str[<span class="number" style="color:#09999;">100</span>];
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">sprintf</span>(str,<span class="string" style="color:#dd1144;">"PI=<span class="variable" style="color:#08080;">%f</span>"</span>,<span style="FONT-WEIGHT: bold" class="keyword">y</span>);
<span class="indent">  </span>outtextxy(<span class="number" style="color:#09999;">280</span>,<span class="number" style="color:#09999;">400</span>,str);
<span class="indent">  </span>getch();
<span class="indent">  </span>closegraph();<span class="regexp" style="color:#09926;">//</span> 关闭绘图窗口
<span class="indent">  </span><span style="FONT-WEIGHT: bold" class="keyword">return</span> <span class="number" style="color:#09999;">0</span>;
}</span>
 类似资料:

相关阅读

相关文章

相关问答