Hello folks,
I am having problems with the OJ C++ compiler. The program below compiles perfectly with the free Borland C++ compiler (Version 5.5.1) and with the GNU g++ compiler version 3.2, but the judge rejects it with a compile error.
The compiler error messages seem strange to me:
01314075_24.c: In method `bool LineSegment::intersects (const
LineSegment &) const':
01314075_24.c:71: `full' undeclared (first use this function)
01314075_24.c:71: (Each undeclared identifier is reported only once for
each function it appears in.)
01314075_24.c:71: parse error before `int'
01314075_24.c:75: `a11' undeclared (first use this function)
01314075_24.c

`intersection' undeclared (first use this function)
01314075_24.c

parse error before `distance'
01314075_24.c:110: `intersectionX' undeclared (first use this function)
01314075_24.c: In method `Rectangle::Rectangle (int, int, int, int)':
01314075_24.c:157: `what' undeclared (first use this function)
01314075_24.c:157: parse error before `means'
01314075_24.c: At top level:
01314075_24.c:162: parse error at end of saved function text
Can someone explain me what's going on here? The error message mentions several identifiers I never declared.
regards,
Franz
[cpp]
/* @JUDGE_ID: my_id 191 C++ "Linear Algebra" */
#include <iostream>
#include <cmath>
inline double sqr( const double x )
{
return x * x;
}
void swap( int * const a, int * const b )
{
int temp = *a;
*a = *b;
*b = temp;
}
inline double distance( const double fromX, const double fromY, const double toX, const double toY )
{
return std::sqrt( sqr( fromX - toX ) + sqr( fromY - toY ) );
}
class Point
{
private:
const int _x;
const int _y;
public:
Point( const int x, const int y ) : _x( x ), _y( y ) {}
const int getX() const
{
return this->_x;
}
const int getY() const
{
return this->_y;
}
};
class LineSegment
{
private:
int _startx, _starty;
int _endx, _endy;
double _distance;
public:
LineSegment( const int startx, const int starty, const int endx, const int endy )
: _startx( startx ), _starty( starty ), _endx( endx ), _endy( endy ),
_distance( distance( static_cast<double>( startx ), static_cast<double>( starty ), static_cast<double>( endx ), static_cast<double>( endy) ) )
{
// normalize: line goes from left to right:
if ( startx > endx ) {
swap( &_startx, &_endx );
swap( &_starty, &_endy );
}
}
bool intersects( const LineSegment & other ) const
{
// Check if both lines segments intersect if they where full lines
int a11 = this->_endx - this->_startx;
int a12 = other._endx - other._startx;
int a21 = this->_endy - this->_starty;
int a22 = other._endy - other._starty;
int determinant = a11 * a22 - a12 * a21;
if ( determinant != 0 ) {
double inverse11 = static_cast<double>( a22 ) / static_cast<double>( determinant );
double inverse12 = static_cast<double>(-a12 ) / static_cast<double>( determinant );
double inverse21 = static_cast<double>(-a21 ) / static_cast<double>( determinant );
double inverse22 = static_cast<double>( a11 ) / static_cast<double>( determinant );
double x1 = inverse11 * static_cast<double>( other._startx - this->_startx )
+ inverse12 * static_cast<double>( other._starty - this->_starty );
double x2 = inverse21 * static_cast<double>( other._startx - this->_startx )
+ inverse22 * static_cast<double>( other._starty - this->_starty );
// Both are line segments, so the calculated intersection point
// must be on the segments and not outside, the distance between the
// intersection point and the edges of the line segment must be
// smaller than the distance between the two edges.
double intersectionX = this->_startx + x1 * ( this->_endx - this->_startx );
double intersectionY = this->_starty + x1 * ( this->_endy - this->_starty );
double dist1 = distance( this->_startx, this->_starty, intersectionX, intersectionY );
double dist2 = distance( this->_endx, this->_endy, intersectionX, intersectionY );
double dist3 = distance( other._startx, other._starty, intersectionX, intersectionY );
double dist4 = distance( other._endx, other._endy, intersectionX, intersectionY );
if ( dist1 > this->_distance || dist2 > this->_distance ) {
return false;
}
if ( dist3 > other._distance || dist4 > other._distance ) {
return false;
}
return true;
}
return false;
}
Point getStartPoint() const
{
return Point( this->_startx, this->_starty );
}
Point getEndPoint() const
{
return Point( this->_endx, this->_endy );
}
};
class Rectangle
{
private:
int _xleft;
int _ybottom;
int _xright;
int _ytop;
public:
Rectangle( const int xleft, const int ybottom, const int xright, const int ytop )
: _xleft( xleft ), _ybottom( ybottom ), _xright( xright ), _ytop( ytop )
{
// It is guaranteed that xleft ybottom actually is that what it means:
if ( xright < xleft ) {
swap( &_xright, &_xright );
}
if ( ytop < ybottom ) {
swap( &_ytop, &_ybottom );
}
}
bool contains( const LineSegment & s ) const
{
Point p1 = s.getStartPoint();
if ( (p1.getX() >= this->_xleft && p1.getX() <= this->_xright) &&
(p1.getY() >= this->_ybottom && p1.getY() <= this->_ytop) ) {
return true;
}
Point p2 = s.getEndPoint();
if ( (p2.getX() >= this->_xleft && p2.getX() <= this->_xright) &&
(p2.getY() >= this->_ybottom && p2.getY() <= this->_ytop) ) {
return true;
}
return false;
}
bool intersects( const LineSegment & with ) const
{
LineSegment a( this->_xleft, this->_ybottom, this->_xright, this->_ybottom );
if ( with.intersects( a ) == true ) {
return true;
}
LineSegment b( this->_xleft, this->_ytop, this->_xright, this->_ytop );
if ( with.intersects( b ) == true ) {
return true;
}
LineSegment c( this->_xright, this->_ytop, this->_xright, this->_ybottom );
if ( with.intersects( c ) == true ) {
return true;
}
LineSegment d( this->_xleft, this->_ytop, this->_xleft, this->_ybottom );
if ( with.intersects( d ) == true ) {
return true;
}
return false;
}
};
int main()
{
int n;
std::cin >> n;
for ( int i = 0; i < n; ++i ) {
// The line:
int startx, starty, endx, endy;
std::cin >> startx >> starty >> endx >> endy;
LineSegment e( startx, starty, endx, endy );
// The rectangle:
int xleft, ytop, xright, ybottom;
std::cin >> xleft >> ybottom >> xright >> ytop;
Rectangle r( xleft, ybottom, xright, ytop );
if ( r.contains(e) || r.intersects(e) ) {
std::cout << 'T' << std::endl;
} else {
std::cout << 'F' << std::endl;
}
}
return 0;
}
[/cpp]