문제

나에게 주어진 과제에 문제가 있습니다. 문제는 나에게 주어진 코드와 비슷한 리소스를 찾을 수 없다는 것입니다.유사점을 찾으려고 너무 많은 문서를 읽었지만 유용한 내용을 찾을 수 없습니다.

이 코드를 이해하고 이를 사용하여 마름모를 만드는 방법을 이해하는 데 도움이 필요합니다. 내가 이해할 수 없는 유일한 것은 Shape 클래스에 속하는 마름모 모양을 만드는 방법입니다.해당 마름모에 중심을 적용한 다음 push_back 방법을 사용하여 정점을 추가합니다. 불행히도 이 푸시백 방법을 사용해야 합니다. 저는 drawLine(10,10,40,10)을 사용하여 시험에 실패했습니다.내가 원하는 곳에 선을 그리는 등.

이번 주 동안 열심히 작업할 것이므로 빨리 응답해야 합니다.

//This is the rhombus.cpp file
#include "rhombus.h"

Rhombus::Rhombus(Vertex point, int radius) : Shape(point)
{
    if((radius>centroid.getX()/2) || (radius>centroid.getY()/2)) // Inteded to be a y?
    {
        cout << "Object must fit on screen." << endl;
        system("pause");
        exit(0);
    }

    Rhombus shape1(20, 20);
    shape1.plotVertices();

}

void Rhombus::plotVertices()
{
    //vertices.push_back(Vertex(centroid.getX(), centroid.getY() + radius));
    //vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
    //vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
    //vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
}

// This is the rhombus.h file
#include "shape.h"

class Rhombus : public Shape 
{
    int radius;
    void plotVertices();
    Rhombus(Vertex point, int radius = 10);
    int area();
    int perimeter();
};

// This is the shape.cpp file
#include "shape.h"

Shape::Shape(Vertex point) : centroid(point)
{
    // constructs a shape

}

void Shape::drawShape()
{

    list<Vertex>::iterator current = vertices.begin();
    list<Vertex>::iterator previous = vertices.begin();
    while(current!=vertices.end())
    {
        Console::gotoXY((*current).getX(),(*current).getY());
        cout << "*";
        if(current!=vertices.begin())
            drawLine((*current).getX(),(*current).getY(), (*previous).getX(),            (*previous).getY());
        previous = current;
        current++;
    }
    previous = vertices.begin();

    //Debug assertion error here.
    drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(),     vertices.front().getY());
}

void Shape::drawLine(int x1, int y1, int x2, int y2)
{      

    bool steep = (abs(y2 - y1) > abs(x2 - x1));
    if(steep)
    {
        swap(x1, y1);
        swap(x2, y2);
    }

    if(x1 > x2)
    {
        swap(x1, x2);
        swap(y1, y2);
    }

    int dx = x2 - x1;
    int dy = abs(y2 - y1);

    float error = dx / 2.0f;
    int ystep = (y1 < y2) ? 1 : -1;
    int y = y1;
    int maxX = x2;

    for(int x=x1; x<maxX; x++)
    {
        if(steep)
        {
            Console::gotoXY(y,x);
            cout << "*";
        }
        else
        {
            Console::gotoXY(x,y);
        cout << "*";
        }
        error -= dy;
        if(error < 0)
        {
            y += ystep;
            error += dx;
        }
    }
}


double Shape::round(double x)
{
    if (ceil(x+0.5) == floor(x+0.5))
    {
        int a = (int) ceil(x);
        if (a%2 == 0)
            return ceil(x);
        else
            return floor(x);
    }
    else 
        return floor(x+0.5);
}

void Shape::outputStatistics()
{

}

// This is the shape.h file
#pragma once
#include "console.h"
#include "vertex.h"
#include <iostream>
#include <list>
#include <cstdlib>
#include <cmath>
using namespace std;

#define PI 3.14159265358979323846

class Shape
{
    list<Vertex>::iterator itr;
protected:
    list<Vertex> vertices;
    Vertex centroid;
    void drawLine(int x1, int y1, int x2, int y2);

    Shape(Vertex point);
    double round(double x);

public:
    void drawShape();
    virtual int area() = 0;
    virtual int perimeter() = 0;
    virtual void outputStatistics();
    void rotate(double degrees);
    void scale(double factor);
};
도움이 되었습니까?

해결책

보시다시피, Rhombus 이미 다음의 하위 클래스입니다. Shape (class Rhombus : public Shape) 따라서 인스턴스를 생성하기만 하면 됩니다. Rhombus 모든 마법이 일어나도록 말이죠.

Shape 다음과 같이 정의됩니다. Vertex 그것에 전달되었습니다 ( point 매개변수)를 사용하여 초기화합니다. centroid 인스턴스 변수가 자동으로;그래서 당신은 사용할 수 있습니다 centroid 내부에서 중심 관련 데이터가 필요한 모든 작업의 ​​중심으로 Shape 또는 다음과 같은 하위 클래스 중 하나에서 Rhombus.

마찬가지로, 목록 vertices 에 사용할 수 있습니다 Shape 모든 하위 클래스를 인스턴스 변수로 사용합니다.나머지 코드를 보면(예: Shape::drawShape), 당신은 방법을 알게 될 것입니다 vertices 현재 모양의 정점을 조작하는 데 사용되었습니다.

여기서 해야 할 일은 그것과 매우 비슷합니다.예를 들어,

Rhombus::Rhombus(Vertex point, int radius) : Shape(point)
{
    if((radius>centroid.getX()/2) || (radius>centroid.getY()/2)) // Inteded to be a y?
    {
        cout << "Object must fit on screen." << endl;
        system("pause");
        exit(0);
    }

    // create vertices for a rhombus with horizontal and vertical diagonals
    vertices.push_back(Vertex(point.getX() - radius, point.getY()));
    vertices.push_back(Vertex(point.getX(), point.getY() - radius));
    vertices.push_back(Vertex(point.getX() + radius, point.getY()));
    vertices.push_back(Vertex(point.getX(), point.getY() + radius));
}

당신이 안에 있을 때 Rhombus::Rhombus (생성자), 당신은 이미 내부에 방금 생성된 마름모;당신은 만들 필요가 없습니다 Rhombus 다시 이의를 제기합니다.정점을 추가하고 중심(이미 완료됨)을 정의하여 인스턴스를 "장식"하기만 하면 됩니다.

당신이 Rhombus 밖으로 Shape;4개의 정점을 생성하고 이를 모든 정점 목록을 추적하는 구조에 추가해야 합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top