问题是:
定义一个Arc类,它绘制一个椭圆的一部分。提示:fl_arc()。
Ellipse是预定义的类,它通过语句在窗口上绘制一个椭圆,例如
Ellipse e1(Point(200,200),50,50);
fl_arc()
是 FLTK 的一部分,我以前在我的Windows机器上安装了它。 由于问题是想通过创建一个名为Arc的新类来绘制椭圆的一部分,我想我应该创建一个具有该名称的新类,并使用Ellipse类的定义并修改它这样它就可以显示椭圆的一部分而不是整个椭圆。这个问题出现在 Programming Principle and practice using C ++ book中,我在那本书中找到的关于Ellipse类的唯一定义是:
struct Ellipse :Shape {
Ellipse(Point p, int w, int h); //center, max and min distance from center
void draw_lines()const;
Point center()const;
Point focus1()const;
Point focus2()const;
void set_major(int ww) {w=ww;}
int major() const {return w;}
void set_minor(int hh) {h=hh;}
int minor() const {return h;}
private:
int w;
int h;
};
我还发现了一个使用书中fl_arc()
的案例,用于绘制这样的圆圈:
fl_arc(point(0).x,point(0).y,r+r,r+r,0,360);
这里 r 是半径。
所以在我看来,我更改了fl_arc()
的参数并编写了波纹管代码,以便给出我想要的问题:
#include <Simple_window.h>
struct arc : Shape {
arc(Point p, int w, int h) // center, min, and max distance from center
: w(w), h(h)
{
add(Point(p.x-w,p.y-h));
}
void draw_lines() const {fl_arc(point(0).x,point(0).y,w+w,h+h,0,120);}
Point center() const { return Point(point(0).x+w,point(0).y+h); }
Point focus1() const { return Point(center().x+int(sqrt(double(w*w-h*h))),center().y); }
Point focus2() const { return Point(center().x-int(sqrt(double(w*w-h*h))),center().y); }
void set_major(int ww) { w=ww; }
int major() const { return w; }
void set_minor(int hh) { h=hh; }
int minor() const { return h; }
private:
int w;
int h;
};
int main()
{
using namespace Graph_lib;
Point t(100,100);
Simple_window win(t,600,400, "semi-ellipse");
arc a(Point(200,200),150,50);
a.draw_lines();
win.wait_for_button();
}
幸运的是,代码运行成功,但它没有显示椭圆的任何部分。
有谁知道为什么?
PS:如果我们能找到修改类的方法,我们可以告诉新类为我们做一些新的工作。
答案 0 :(得分:1)
以下是一种可能的实现,您可以使用class Ellipse
的现有工具覆盖函数draw_lines()
,以定义class Arc
:
#include"Simple_window.h"
#include"Graph.h"
#include<iostream>
using namespace Graph_lib;
//---------------------------------------------------------------------------
//Class Arc
namespace Graph_lib{
class Arc : public Ellipse {
public:
Arc(Point p, int w, int h, int arc_n, int arc_x) : Ellipse(p,w,h), arc_min(arc_n), arc_max(arc_x) {}
void draw_lines() const;
private:
int arc_min;
int arc_max;
};
void Arc::draw_lines() const
{
if(color().visibility())
fl_arc(point(0).x,point(0).y, major()*2, minor()*2, arc_min, arc_max);
}
}
//---------------------------------------------------------------------------
int main()
try
{
Simple_window win(Point(100,100), 800, 600, "Exercise #1");
Graph_lib::Arc arc1(Point(100,100),50,50,0,90);
win.attach(arc1);
win.wait_for_button();
}
catch(exception& e)
{
std::cout << e.what() << std::endl;
return 1;
}
catch(...)
{
std::cout << "unknown error..." << std::endl;
return 2;
}
编程 - 使用C ++的原理和实践(第477页,练习1) Francisco Tavares的解决方案
答案 1 :(得分:0)
虽然我可以找到该代码的错误但我仍然有一些关于该练习的问题,我想在你们的专业人士中提及它们。
如果行a.draw_lines();
将被此行win.attach(a);
替换,则问题会成功运行。
剩下的问题是:
1-现在当我在上面的代码中使用名称“ Arc ”而不是“ arc ”时,我收到此错误。
错误8错误C2146:语法错误:缺少';'在标识符'a'c之前:\ users \ cs \ documents \ visual studio 2012 \ projects \ test2 \ test2 \ test2.cpp 25 错误10 错误C3861:'a':找不到标识符c:\ users \ cs \ documents \ visual studio 2012 \ projects \ test2 \ test2 \ test2.cpp 25
2-问题是我们想要定义一个类(不是结构)所以当我用类替换 struct 并将关键字 public 在class arc : Shape {
之后,我收到此错误。
*错误8错误C2243:'type cast':从'arc *'转换为'Graph_lib :: Shape&amp;'存在但无法访问c:\ users \ cs \ documents \ visual studio 2012 \ projects \ test2 \ test2 \ test2.cpp 29 错误9错误C2243:'type cast':从'arc '转换为'Graph_lib :: Shape&amp;'存在但无法访问c:\ users \ cs \ documents \ visual studio 2012 \ projects \ test2 \ test2 \ test2.cpp 30
有什么想法吗?