【发布时间】:2012-03-01 02:08:17
【问题描述】:
我已经为此工作了大约一个小时,但我无法弄清楚我做错了什么。这是问题的问题陈述:
沿窗口的一条对角线绘制一系列圆圈。圆圈 应该是不同的颜色,每个圆圈应该接触(但不是 重叠)它上面和下面的那个。允许程序用户 确定要绘制多少个圆圈。
这些是给我的一些提示:
您会发现在放置几何元素时所涉及的几何 如果你把你的窗户做成方形,对角线会更容易。而不是使用 getmaxheight() 和 getmaxwidth(),考虑使用 getmaxheight() 两个维度。
计算距离时不要忘记勾股定理 您的代码,例如对角线的长度。不过请记住, 屏幕上的单位是像素,所以 计算用处不大。这绝对是一个地方 整数运算。
使用您要绘制的元素数量(正方形、圆形、 等)将总长度分成多个步骤,以便循环工作 与。
当您知道要绘制多少以及要绘制什么时,使用 for 循环绘制图形 它们的大小。确定循环前的计数和大小。
到目前为止,这是我创建的代码。输入 4 个圆圈仅在屏幕上绘制 3 个,第三个部分在屏幕外。圆圈也没有接触,这对我来说毫无意义,因为将下一个圆圈的中心向下移动直径的长度应该使两个圆圈接触。这是我的代码:
#include <graphics.h>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int foreColor;
int diagLength;
int radius,diameter;
int centerX = 0, centerY = 0;
int numCircles; // number of circles.
int width, height; // screen width and height
cout << "Enter number of circles: ";
cin >> numCircles;
width = getmaxheight();
height = getmaxheight();
initwindow(width, height, "Circles");
diagLength = sqrt((width * width) + (height * height));
diameter = diagLength / numCircles;
radius = diameter / 2;
centerX = radius;
centerY = radius;
for (int i = 1; i <= numCircles; i++)
{
foreColor = i % 16; // 0 <= foreColor <= 15
setcolor(foreColor);
setfillstyle(i % 12, foreColor); // Set fill style
fillellipse(centerX, centerY, radius, radius);
centerX = centerX + diameter;
centerY = centerY + diameter;
}
getch(); // pause for user
closegraph();
}
【问题讨论】:
-
1) 别管图形,看看数字——它们看起来对吗? 2) 尝试在
center点上画点,看看它们的间距是否正确。 3) 将圆点替换为圆圈,看看它们接触不上的严重程度——是几个像素,还是半径的一半? -
我在我的身上试过这个,#include
在名称下抛出一条红线。我认为这是因为它没有内置在链接器中,但我不确定在哪里包含包含?