2012-08-04

SCIP in C++11 ― 2.1.2節


有向線分と長方形:問題2.22.3

listtupleを使ってた頃は型の構造はまんま型の指定に現れていたが、
shared_ptrtypedefしたListにしてからは、ぱっと見分からないので、
なるべくtypedefして何を扱っているのかわかりやすくした。
これはSchemeではあまり見ない利点かも。
----
const double average(const double x1, const double x2)
{return((x1+x2)/2.0);}

//Point
typedef List Point;

const Point makePoint(const double x, const double y)
{return(cons(x,y));}
const double xPoint(const Point p){return(car(p)->getDouble());}
const double yPoint(const Point p){return(cadr(p)->getDouble());}
void printPoint(const Point p)
{
    cout<<"("<<xPoint(p)<<","<<yPoint(p)<<")";
}

//Segment
typedef List Segment;
const Segment makeSegment
(const Point startPoint, const Point endPoint)
{return(makeList(startPoint,endPoint));}
const Point startSegment(const Segment s){return(car(s));}
const Point endSegment(const Segment s){return(cadr(s));}
const Point midPointSegment(const Segment s)
{
    return(makePoint
           (average(xPoint(startSegment(s)),xPoint(endSegment(s))),
            average(yPoint(startSegment(s)),yPoint(endSegment(s)))));
}
const double segmentLength(const Segment s)
{return(sqrt(pow(xPoint(startSegment(s))-xPoint(endSegment(s)),2)
             +pow(yPoint(startSegment(s))-yPoint(endSegment(s)),2)));}


// Rectangle1: definition with 2 diagonal points
typedef List Rectangle;
const Rectangle makeRectangle
(const Point vertex1, const Point vertex3)
{return(makeList(vertex1,vertex3));}
// Rectangle2: definition with a left-lower point and 2 segment lengths
const Rectangle makeRectangle
(const Point vertex3, const double verticalLength,
 const double horizontalLength)
{return(makeRectangle(makePoint(xPoint(vertex3)+horizontalLength,
                                yPoint(vertex3)+verticalLength),
                      vertex3));
}

const Point vertex1Rectangle(const Rectangle rectangle)
{return(car(rectangle));}
const Point vertex3Rectangle(const Rectangle rectangle)
{return(cadr(rectangle));}
const Point vertex2Rectangle(const Rectangle rectangle)
{return(makePoint(xPoint(vertex1Rectangle(rectangle))
                  ,yPoint(vertex3Rectangle(rectangle))));}
const Point vertex4Rectangle(const Rectangle rectangle)
{return(makePoint(xPoint(vertex3Rectangle(rectangle)),
                  yPoint(vertex1Rectangle(rectangle))));}
const Segment verticalSide(const Rectangle rectangle)
{
    const Point vertex1(vertex1Rectangle(rectangle));
    const Point vertex2(vertex2Rectangle(rectangle));
    return(makeSegment(vertex1,vertex2));
}
const Segment horizontalSide(const Rectangle rectangle)
{
    const Point vertex1(vertex1Rectangle(rectangle));
    const Point vertex4(vertex4Rectangle(rectangle));
    return(makeSegment(vertex1,vertex4));
}
const double perimeter(const Rectangle rectangle)
{return(2.0*(segmentLength(verticalSide(rectangle))
             +segmentLength(horizontalSide(rectangle))));
}
const double area(const Rectangle rectangle)
{return((segmentLength(verticalSide(rectangle))
         *segmentLength(horizontalSide(rectangle))));
}


int main(int argc, char** argv)
{

    cout<<"Excersize 2.2:"<<endl;
    const Point A(makePoint(1,2));
    const Point B(makePoint(-1,-1));
   
    cout<<"A";
    printPoint(A);
    cout<<endl;
    cout<<"B";
    printPoint(B);
    cout<<endl;

    const Segment AB(makeSegment(A,B));

    cout<<"the middle point of AB:";
    printPoint(midPointSegment(AB));
    cout<<endl;

    cout<<endl<<"Excersize 2.3:"<<endl
        <<"Rectangle1 (definition with 2 diagonal points):"<<endl;
    const Rectangle rectangle1=makeRectangle(A,B);
    cout<<"perimeter="<<perimeter(rectangle1)<<endl
        <<"area="<<area(rectangle1)<<endl;

    cout<<endl<<
        "Rectangle2 (definition with a left-lower point"<<endl
        <<"and 2 side lengths):"
        <<endl;
    const Rectangle rectangle2=makeRectangle(B,3.0,2.0);
    cout<<"perimeter="<<perimeter(rectangle2)<<endl
        <<"area="<<area(rectangle2)<<endl;

    return(0);
}
----
出力
----
Excersize 2.2:
A(1,2)
B(-1,-1)
the middle point of AB:(0,0.5)

Excersize 2.3:
Rectangle1 (definition with 2 diagonal points):
perimeter=10
area=6

Rectangle2 (definition with a left-lower point
and 2 side lengths):
perimeter=10
area=6

0 件のコメント :

コメントを投稿