kokolwiek Temat założony przez niniejszego użytkownika |
mam problem z kodem w c++ » 2024-12-20 13:18:40 napisałem pliki ksztalt.h: #ifndef KSZTALTY_H #define KSZTALTY_H #include <Gl/gl.h> #include <windows.h> #include <vector> #include <cmath> enum Drawing_Layout { None = 0 << 0, up_down = 1 << 0, down_up = 1 << 1, right_left = 1 << 2, left_right = 1 << 3, inside = 1 << 4, outside = 1 << 5 }; class Color { public: Color() = default; Color( float r, float g, float b ); virtual ~Color(); float r, g, b; protected: private: }; class Point { public: Point() = default; Point( float x, float y ) : x( x ) , y( y ) { } Point operator +( const Point & p ) const; Point operator -( const Point & p ) const; Point operator *( const Point & p ) const; Point operator /( const Point & p ) const; Point & operator =( const Point & p ); float length() const; Point normalize() const; float get_x() const; float get_y() const; virtual ~Point(); protected: private: float x, y; }; class Polygon { public: Polygon() = default; Polygon( const std::vector < Point > & points ) : vertices( points ) { } virtual ~Polygon(); Point get_point( int i ) const; void set_point( int num, const Point & p ); void add_point( const Point & p ); void add_point( const float & x, const float & y ); void del_point( int num ); virtual void rotate( float angle_deg, const Point & center ); void draw( Color c, GLenum mode ); std::vector < Point > vertices; Point velocity; protected: private: }; class Quadral : public Polygon { public: Quadral() = default; Quadral( Point A, Point B, Point C, Point D ); Quadral( Point poz, Point size ); virtual ~Quadral(); Point pozition; void draw( Color c0, Color c1, Drawing_Layout layout ); protected: private: }; class Triangle : public Polygon { public: Triangle() = default; Triangle( Point A, Point B, Point C ); Triangle( Point poz, Point size ); virtual ~Triangle(); Point pozition; void draw( Color c0, Color c1, Drawing_Layout layout ); void draw( Color c0, Color c1, Color c2 ); protected: private: }; class Ellipses : public Polygon { public: Ellipses() = default; Ellipses( Point poz, Point size, int segments = 333 ); virtual ~Ellipses(); Point pozition, r; void draw( Color c0, Color c1, Drawing_Layout layout ); protected: private: }; #endif plik ksztalt.cpp #include "ksztalty.h" Color::Color( float r, float g, float b ) { this->r = r; this->g = g; this->b = b; } Color::~Color() { } Point Point::operator +( const Point & p ) const { return Point( this->x + p.x, this->y + p.y ); } Point Point::operator -( const Point & p ) const { return Point( this->x - p.x, this->y - p.y ); } Point Point::operator *( const Point & p ) const { return Point( this->x * p.x, this->y * p.y ); } Point Point::operator /( const Point & p ) const { return Point( this->x / p.x, this->y / p.y ); } Point & Point::operator =( const Point & p ) { if( this != & p ) { this->x = p.x; this->y = p.y; } return * this; } float Point::get_x() const { return x; } float Point::get_y() const { return y; } Point Point::normalize() const { float len = length(); return { x / len, y / len }; } float Point::length() const { return std::sqrt( x * x + y * y ); } Point::~Point() { } Point Polygon::get_point( int i ) const { return vertices.at( i ); }
void Polygon::set_point( int num, const Point & p ) { if( num >= 0 && num < vertices.size() ) { vertices[ num ] = p; } } void Polygon::add_point( const Point & p ) { vertices.push_back( p ); } void Polygon::add_point( const float & x, const float & y ) { vertices.push_back( Point( x, y ) ); } void Polygon::del_point( int num ) { if( num >= 0 && num < vertices.size() ) { vertices.erase( vertices.begin() + num ); } } void Polygon::rotate( float angle_deg, const Point & center ) { float angle_rad = angle_deg * M_PI / 180.0f; for( auto & point: vertices ) { float temp_x = point.get_x() - center.get_x(); float temp_y = point.get_y() - center.get_y(); float rotated_x = temp_x * cos( angle_rad ) - temp_y * sin( angle_rad ); float rotated_y = temp_x * sin( angle_rad ) + temp_y * cos( angle_rad ); point = Point( rotated_x + center.get_x(), rotated_y + center.get_y() ); } } void Polygon::draw( Color c, GLenum mode ) { if( this->vertices.empty() ) return; glBegin( mode ); for( int i = 0; i < this->vertices.size(); i++ ) { glColor3f( c.r, c.g, c.b ); glVertex2f( this->vertices[ i ].get_x(), this->vertices[ i ].get_y() ); } glEnd(); } Polygon::~Polygon() { } Quadral::Quadral( Point A, Point B, Point C, Point D ) : Polygon( std::vector < Point > { } ) { add_point( A ); add_point( B ); add_point( C ); add_point( D ); this->pozition = Point(( A.get_x() + B.get_x() + C.get_x() + D.get_x() ) / 4,( A.get_y() + B.get_y() + C.get_y() + D.get_y() ) / 4 ); } Quadral::Quadral( Point poz, Point size ) : Polygon( std::vector < Point > { } ) { add_point( Point( poz.get_x() - size.get_x() / 2, poz.get_y() - size.get_y() / 2 ) ); add_point( Point( poz.get_x() + size.get_x() / 2, poz.get_y() - size.get_y() / 2 ) ); add_point( Point( poz.get_x() - size.get_x() / 2, poz.get_y() + size.get_y() / 2 ) ); add_point( Point( poz.get_x() + size.get_x() / 2, poz.get_y() + size.get_y() / 2 ) ); this->pozition = poz; } void Quadral::draw( Color c0, Color c1, Drawing_Layout layout ) { if( this->vertices.empty() ) return; switch( layout ) { case up_down: glBegin( GL_QUAD_STRIP ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glVertex2f( vertices[ 3 ].get_x(), vertices[ 3 ].get_y() ); glEnd(); break; case down_up: glBegin( GL_QUAD_STRIP ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glVertex2f( vertices[ 3 ].get_x(), vertices[ 3 ].get_y() ); glEnd(); break; case right_left: glBegin( GL_QUAD_STRIP ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glVertex2f( vertices[ 3 ].get_x(), vertices[ 3 ].get_y() ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glEnd(); break; case left_right: glBegin( GL_QUAD_STRIP ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glVertex2f( vertices[ 3 ].get_x(), vertices[ 3 ].get_y() ); glEnd(); break; case inside: glBegin( GL_TRIANGLE_FAN ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( pozition.get_x(), pozition.get_y() ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glVertex2f( vertices[ 3 ].get_x(), vertices[ 3 ].get_y() ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glVertex2f( vertices[ 3 ].get_x(), vertices[ 3 ].get_y() ); glEnd(); break; case outside: glBegin( GL_TRIANGLE_FAN ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( pozition.get_x(), pozition.get_y() ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glVertex2f( vertices[ 3 ].get_x(), vertices[ 3 ].get_y() ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glVertex2f( vertices[ 3 ].get_x(), vertices[ 3 ].get_y() ); glEnd(); break; default: Polygon::draw( c0, GL_QUAD_STRIP ); break; } } Quadral::~Quadral() { } Triangle::Triangle( Point A, Point B, Point C ) : Polygon( std::vector < Point > { } ) { add_point( A ); add_point( B ); add_point( C ); this->pozition = Point(( A.get_x() + B.get_x() + C.get_x() ) / 3,( A.get_y() + B.get_y() + C.get_y() ) / 3 ); } Triangle::Triangle( Point poz, Point size ) : Polygon( std::vector < Point > { } ) { add_point( poz.get_x() - size.get_x() / 2, poz.get_y() - size.get_y() / 3 ); add_point( poz.get_x() + size.get_x() / 2, poz.get_y() - size.get_y() / 3 ); add_point( poz.get_x(), poz.get_y() + 2 * size.get_y() / 3 ); this->pozition = poz; } void Triangle::draw( Color c0, Color c1, Color c2 ) { if( this->vertices.empty() ) return; glBegin( GL_TRIANGLES ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glColor3f( c2.r, c2.g, c2.b ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glEnd(); } void Triangle::draw( Color c0, Color c1, Drawing_Layout layout ) { if( this->vertices.empty() ) return; switch( layout ) { case up_down: glBegin( GL_TRIANGLE_STRIP ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glEnd(); break; case down_up: glBegin( GL_TRIANGLE_STRIP ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glEnd(); break; case right_left: glBegin( GL_TRIANGLE_STRIP ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glColor3f( c1.r + c0.r / 2, c1.g + c0.r / 2, c1.b + c0.b / 2 ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glEnd(); break; case left_right: glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glColor3f( c1.r + c0.r / 2, c1.g + c0.r / 2, c1.b + c0.b / 2 ); glVertex2f( vertices[ 2 ].get_x(), vertices[ 2 ].get_y() ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 1 ].get_x(), vertices[ 1 ].get_y() ); glEnd(); break; default: Polygon::draw( c0, GL_TRIANGLE_STRIP ); break; } } Triangle::~Triangle() { } Ellipses::Ellipses( Point poz, Point size, int segments ) : Polygon( std::vector < Point > { } ) { this->pozition = poz; this->r = size; add_point( pozition ); for( int i = 0; i <= segments; ++i ) { float angle = i * 2.0f * 3.14159f / segments; float x = r.get_x() * cos( angle ) * ( float ) GetSystemMetrics( SM_CYSCREEN ) / GetSystemMetrics( SM_CXSCREEN ); float y = r.get_y() * sin( angle ); add_point( Point( x, y ) + pozition ); } } void Ellipses::draw( Color c0, Color c1, Drawing_Layout layout ) { if( this->vertices.empty() ) return; switch( layout ) { case inside: glBegin( GL_TRIANGLE_FAN ); glColor3f( c0.r, c0.g, c0.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glColor3f( c1.r, c1.g, c1.b ); for( int i = 1; i <= this->vertices.size(); i++ ) glVertex2f( vertices[ i ].get_x(), vertices[ i ].get_y() ); glEnd(); break; case outside: glBegin( GL_TRIANGLE_FAN ); glColor3f( c1.r, c1.g, c1.b ); glVertex2f( vertices[ 0 ].get_x(), vertices[ 0 ].get_y() ); glColor3f( c0.r, c0.g, c0.b ); for( int i = 1; i <= this->vertices.size(); i++ ) glVertex2f( vertices[ i ].get_x(), vertices[ i ].get_y() ); glEnd(); break; default: Polygon::draw( c0, GL_TRIANGLE_FAN ); break; } } Ellipses::~Ellipses() { }
i plik Collision.h #ifndef COLLISION_H #define COLLISION_H #include <typeinfo> #include "ksztalty.h" class Collision { public: Collision( Polygon * obj, std::vector < Polygon * > p ); virtual ~Collision(); void colaider(); protected: private: Polygon obj; std::vector < Polygon > p; };
#endif nie wiem czemu dostaję błędy C:\Users\Adam\Documents\projekty\open GL\Collision.h|8|error: expected ')' before '*' token| C:\Users\Adam\Documents\projekty\open GL\Collision.h|14|error: 'Polygon' does not name a type| C:\Users\Adam\Documents\projekty\open GL\Collision.h|15|error: type/value mismatch at argument 1 in template parameter list for 'template<class _Tp, class _Alloc> class std::vector'| C:\Users\Adam\Documents\projekty\open GL\Collision.h|15|error: template argument 2 is invalid| C:\Users\Adam\Documents\projekty\open GL\Collision.cpp|2|error: expected constructor, destructor, or type conversion before '(' token| C:\Users\Adam\Documents\projekty\open GL\Collision.cpp|9|error: 'end' was not declared in this scope; did you mean 'std::end'?| C:\Users\Adam\Documents\projekty\open GL\Collision.cpp|10|error: cannot 'dynamic_cast' 'poly' (of type '<type error>') to type 'class Ellipses*' (source is not a pointer)| |