質問

The problem says:

Define a class Arc, which draws a part of an ellipse. Hint: fl_arc().

Ellipse is predefined class which draws an ellipse on the window by a statement, e.g., Ellipse e1(Point(200,200),50,50); and fl_arc() is part of FLTK which I've previously installed it on my Windows machine. Since the problem has wanted to draw a part of an ellipse by creating a new class named Arc, I think I should make a new class with that name and also use the definition of the class Ellipse and modify it so that it shows a part of an ellipse instead of whole of an ellipse as wanted. This problem is in Programming Principle and practice using C++ book and the only definition that I found in that book about the Ellipse class was this:

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;
};

and also I found a case of using the fl_arc() in the book which was used for drawing a Circle like this:

fl_arc(point(0).x,point(0).y,r+r,r+r,0,360);

Here r is radius. So on my think, I changed the parameters of the fl_arc() and wrote the bellow code to give me what the problem has wanted:

#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();
}

The code runs successfully fortunately but it doesn't show any part of an ellipse.

Question:

Does anyone know why?

PS: If we can find some way for modifying a class we can tell that new class to does some new work for us.

役に立ちましたか?

解決 2

Although I could to find the bug of that code but still I have some issues about that exercise that I like to mention them among you professional guys.

If the line a.draw_lines(); will be replaced by this line win.attach(a); the problem runs successfully.

The remained problems are these:

1- Now when I use the name "Arc" instead of the "arc" in above code I get this error.

Error 8 error C2146: syntax error : missing ';' before identifier 'a' c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 25 Error 10 error C3861: 'a': identifier not found c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 25

2- The problem has wanted us to define a class (not a struct) so when I replace struct with class and put keyword public just after class arc : Shape {, I get this error.

*Error 8 error C2243: 'type cast' : conversion from 'arc *' to 'Graph_lib::Shape &' exists, but is inaccessible c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 29 Error 9 error C2243: 'type cast' : conversion from 'arc ' to 'Graph_lib::Shape &' exists, but is inaccessible c:\users\cs\documents\visual studio 2012\projects\test2\test2\test2.cpp 30

Any idea?

他のヒント

Here is one possible implementation, where you use the already existing facilities of class Ellipse, overwriting the function draw_lines(), to define the 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;
}

Programming - Principles and Practice Using C++ (pg.477, exercise 1) solution by Francisco Tavares

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top