2014-12-04 48 views
-5

我使用的语言没有内置的创建任何类型图表的能力。以下代码我找到了here。这是非常古老的C++代码。定位饼片问题

圆(原点)=(H,K)

半径= R

... 
void Circular_arc(constint h, constint k, constint r, constint start_angle, constint end_angle) 
{ 
    int color = getcolor(); 

    float angle = (((start_angle <= end_angle) ? start_angle : end_angle)*(M_PI/180)); 
    float range = (((end_angle > start_angle) ? end_angle : start_angle)*(M_PI/180)); 

    float x = (r*cos(angle)); 
    float y = (r*sin(angle)); 

    do 
    { 
     putpixel((int)(h + x + 0.5), (int)(k - y + 0.5), color); 

     angle += 0.001; 

     x = (r*cos(angle)); 
     y = (r*sin(angle)); 
    } while (angle <= range); 
} 
... 

的中心I它转换为我使用的语言。我把它放在一个运行的循环中。

代码的问题是切片总是从相同的位置开始,并绘制先前绘制的内容。

最终的结果看起来是这样的:

enter image description here

您可以从屏幕上不同颜色的数字看到,有3个被掩盖了其他弧线。首先绘制黑色弧线。然后红色。然后绿色。黄色的那个。随着弧线的绘制,它们会覆盖之前绘制的弧线。我已经倒转了角度的长度,以表明实际上有4个弧线。再次,黑色先拉出,然后是红色,然后是绿色,然后是黄色。

enter image description here

有没有开始在当前饼片的终点的下一个扇形的方式?

+0

WOW! 5票反对,没有人投入一个词来帮助说明为什么这是一个糟糕的职位。你们很棒! – 2014-12-04 19:33:36

+1

[This](http://meta.stackoverflow.com/questions/266370/consideration-for-removing-the-downvote-button-from-questions)是我与StackOverflow有爱/恨关系的原因之一。但是你的问题合法性很差。它没有任何人分析的帖子中的代码。当你创建一个调试问题时,它必须遵守[this](http://stackoverflow.com/help/mcve)标准。 – 2014-12-04 19:45:21

+0

@MichaelHarvey标准C++中没有'gotoxy'这样的函数。你使用什么编译器? – PaulMcKenzie 2014-12-04 19:46:48

回答

0

我修改了这个并创建了一个方法。我做了以下来解决我的问题。首先,我摆脱了通过创建另一个函数将起始角度和结束角度从度数转换为弧度的过程,该函数只将结束角度转换为弧度。在调用Circular_arc之后,我将start_angle设置为等于end_angle的值。编写/使用的代码较少。其次,我删除了第一次找到x,y的计算,并将第二个移到了循环中的第一个。这不是必需的,但我是尽可能少编写代码的忠实粉丝。我很懒。 OP的第三大部分和全部原因首先是当前弧线和前一弧线的结束。为此,我创建了一个变量并将其设置为等于start_angle的值。然后我将do-while循环中的参数设置为while (start_angle <= (end_angle+angle))。这在上一个弧的末尾开始了当前弧。

以下是我将代码转换为C++的尝试。如果有错误,请让我知道,我会尽我所能解决它们。我希望这将有助于未来的人!

... 
void Circular_arc(constint h, constint k, constint r, constint start_angle, constint end_angle) 
{ 
    float angle 
    angle = start_angle 
    do 
    { 

     x = (r*cos(start_angle)); 
     y = (r*sin(start_angle)); 

     putpixel((int)(h + x + 0.5), (int)(k - y + 0.5), getcolor()); 

     angle += 0.001; 


    } while (start_angle <= (end_angle+angle)); 
} 
... 

这里的饼形图的样子那么远,

enter image description here

0

因为我真的不理解Turbo C++,所以我不能肯定地说。但我认为这是您的解决方案:

void Circular_arc(constint h, constint k, constint r, constint start_angle, constint end_angle) 
{ 
    static int offset = 0; 

    start_angle = (start_angle + offset) % 360; 
    end_angle = (end_angle + offset) % 360; 
    offset = end_angle; 

    int color = getcolor(); 

    float angle = (((start_angle <= end_angle) ? start_angle : end_angle)*(M_PI/180)); 
    float range = (((end_angle > start_angle) ? end_angle : start_angle)*(M_PI/180)); 

    float x = (r*cos(angle)); 
    float y = (r*sin(angle)); 

    do 
    { 
     putpixel((int)(h + x + 0.5), (int)(k - y + 0.5), color); 

     angle += 0.001; 

     x = (r*cos(angle)); 
     y = (r*sin(angle)); 
    } while (angle <= range); 
} 

也让我说,这是一个可怕的解决方案,因为它是面向没有办法的对象。但它似乎是最好的,你可以做,而无需重新开始(你应该这样做)。