Skip to content

Instantly share code, notes, and snippets.

@PG-kura
Created December 30, 2011 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PG-kura/1540729 to your computer and use it in GitHub Desktop.
Save PG-kura/1540729 to your computer and use it in GitHub Desktop.
「Sony Japan - GO FOR IT -」
#include "main.hpp"
#include <assert.h>
typedef segment_3d C;
template < bool is_nega >
C make_C( int x, int y, int z, unsigned w, unsigned h, unsigned d )
{
point_t p( x, y, z );
return C( is_nega? - p: p, length_t( w, h, d ) );
}
// c0 と c1 が同一の場合は重複
template < bool is_nega >
void test1()
{
C c0 = make_C< is_nega >( 1, 2, 3, 4, 5, 6 );
C c1 = c0;
assert( is_overlapped( c0, c1 ) );
}
// c0 が c1 に空間を開けて内包される場合は重複
template < bool is_nega >
void test2()
{
C c0 = make_C< is_nega >( 2, 2, 2, 6, 6, 6 );
C c1 = make_C< is_nega >( 0, 0, 0, 10, 10, 10 );
assert( is_overlapped( c0, c1 ) );
}
// c0 が c1 の始点側に内接する場合は重複
template < bool is_nega >
void test3()
{
C c0 = make_C< is_nega >( 4, 5, 6, 1, 2, 3 );
C c1 = make_C< is_nega >( 4, 4, 6, 2, 4, 4 );
assert( is_overlapped( c0, c1 ) );
}
// c0 が c1 の終点側に内接する場合は重複
template < bool is_nega >
void test4()
{
C c0 = make_C< is_nega >( 5, 5, 5, 1, 2, 3 );
C c1 = make_C< is_nega >( 4, 4, 4, 2, 3, 4 );
assert( is_overlapped( c0, c1 ) );
}
// c0 と c1 が x, y, z それぞれで交わっている場合は重複
template < bool is_nega >
void test5()
{
C c0 = make_C< is_nega >( 1, 2, 3, 4, 5, 6 );
C c1 = make_C< is_nega >( 3, 4, 5, 6, 7, 8 );
assert( is_overlapped( c0, c1 ) );
}
// c0 と c1 が x だけで交わっている場合は重複とみなさない。
template < bool is_nega >
void test6()
{
C c0 = make_C< is_nega >( 1, 2, 3, 4, 5, 6 );
C c1 = make_C< is_nega >( 2, 8, 10, 4, 5, 5 );
assert( ! is_overlapped( c0, c1 ) );
}
// c0 と c1 が y だけで交わっている場合は重複とみなさない。
template < bool is_nega >
void test7()
{
C c0 = make_C< is_nega >( 1, 2, 3, 4, 5, 6 );
C c1 = make_C< is_nega >( 6, 3, 10, 5, 5, 5 );
assert( ! is_overlapped( c0, c1 ) );
}
// c0 と c1 が z だけで交わっている場合は重複とみなさない。
template < bool is_nega >
void test8()
{
C c0 = make_C< is_nega >( 1, 2, 3, 4, 5, 6 );
C c1 = make_C< is_nega >( 6, 8, 4, 5, 5, 6 );
assert( ! is_overlapped( c0, c1 ) );
}
template < bool is_nega >
void tests()
{
test1< is_nega >();
test2< is_nega >();
test3< is_nega >();
test4< is_nega >();
test5< is_nega >();
test6< is_nega >();
test7< is_nega >();
test8< is_nega >();
}
int main()
{
// x, y, z をそれぞれそのまま使ってテスト
tests< false >();
// x, y, z をそれぞれ反転させてテスト
tests< true >();
}
// x, y, z の値を持つ型。
template < typename T >
struct point_3d
{
point_3d( T x, T y, T z )
: x_( x )
, y_( y )
, z_( z )
{}
point_3d( point_3d const & o )
: x_( o.x_ )
, y_( o.y_ )
, z_( o.z_ )
{}
T x_, y_, z_;
};
template < typename T >
point_3d< T > operator - ( point_3d< T > const & a )
{
return point_3d< T >( - a.x_, - a.y_, - a.z_ );
}
typedef point_3d< int > point_t;
typedef point_3d< unsigned > length_t;
// 点と距離を持つ型。
template < typename T_pos, typename T_length >
class segment
{
public:
segment( T_pos const & pos, T_length const & size )
: pos_ ( pos )
, size_( size )
{}
segment( segment const & o )
: pos_ ( o.pos_ )
, size_( o.size_ )
{}
T_pos end_pos() const
{
return pos_ + size_;
}
T_pos pos_;
T_length size_;
};
// 線分型
typedef segment< int, unsigned > segment_1d;
// 直方体型
typedef segment< point_t, length_t > segment_3d;
// 線分の重複がある場合に true を返す。
bool is_overlapped( segment_1d const & a, segment_1d const & b )
{
if( a.pos_ == b.pos_ ) { return true; }
if( a.pos_ < b.pos_ ) { return a.end_pos() >= b.pos_; }
{ return b.end_pos() >= a.pos_; }
}
// 直方体の重複がある場合に true を返す。
bool is_overlapped( segment_3d const & a, segment_3d const & b )
{
return is_overlapped( segment_1d( a.pos_.x_, a.size_.x_ )
, segment_1d( b.pos_.x_, b.size_.x_ ) )
&& is_overlapped( segment_1d( a.pos_.y_, a.size_.y_ )
, segment_1d( b.pos_.y_, b.size_.y_ ) )
&& is_overlapped( segment_1d( a.pos_.z_, a.size_.z_ )
, segment_1d( b.pos_.z_, b.size_.z_ ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment