一个矢量类Vector
1.定义一个矢量类Vector,其结构如下:私有数据成员intx,y;表示二维矢量的两个分量构造函数实现初始化数据成员输出...
1. 定义一个矢量类Vector,其结构如下:
私有数据成员int x,y; 表示二维矢量的两个分量
构造函数实现初始化数据成员
输出数据成员函数 void display( );
类外访问数据成员函数
int getx( );
int gety( );
功能成员函数实现矢量加、减、乘运算
Vector Add(const Vector &ob2 );
Vector Sub(const Vector &ob2 );
int Mult(const Vector &ob2 );
2. 在Vector 类的基础上,定义矩阵类Matrix(2*2阶),其结构如下:
私有数据成员Vector lefttop,righttop,leftbottom,rightbottom; 表示2*2阶矩阵的四个元素。(提示:也可以用子对象数组来实现Vector MatrixArray[2][2];)
构造函数实现初始化数据成员
输出数据成员函数 void display( );
类外访问数据成员函数
Vector GetVector(int i, int j); // i, j为矩阵元素的下标MatrixArray[i][j]
功能成员函数实现矢量加、减、乘运算
Matrix Add(const Matrix &ob2 );
Matrix Sub(const Matrix &ob2 );
Matrix Mult(const Matrix &ob2 );
3. 其他要求:
主函数实现对以上两个类中定义的功能的验证
类中定义的成员函数的函数体在类外完成
我希望得到2的答案 展开
私有数据成员int x,y; 表示二维矢量的两个分量
构造函数实现初始化数据成员
输出数据成员函数 void display( );
类外访问数据成员函数
int getx( );
int gety( );
功能成员函数实现矢量加、减、乘运算
Vector Add(const Vector &ob2 );
Vector Sub(const Vector &ob2 );
int Mult(const Vector &ob2 );
2. 在Vector 类的基础上,定义矩阵类Matrix(2*2阶),其结构如下:
私有数据成员Vector lefttop,righttop,leftbottom,rightbottom; 表示2*2阶矩阵的四个元素。(提示:也可以用子对象数组来实现Vector MatrixArray[2][2];)
构造函数实现初始化数据成员
输出数据成员函数 void display( );
类外访问数据成员函数
Vector GetVector(int i, int j); // i, j为矩阵元素的下标MatrixArray[i][j]
功能成员函数实现矢量加、减、乘运算
Matrix Add(const Matrix &ob2 );
Matrix Sub(const Matrix &ob2 );
Matrix Mult(const Matrix &ob2 );
3. 其他要求:
主函数实现对以上两个类中定义的功能的验证
类中定义的成员函数的函数体在类外完成
我希望得到2的答案 展开
展开全部
#ifndef OLI_VECTOR_H
#define OLI_VECTOR_H
/*
------------------------------------------------------------------
File: Vector.h
Started: 09/01/2004 22:20:40
$Header: $
$Revision: $
$Locker: $
$Date: $
Author: Olivier renault
------------------------------------------------------------------
Module:
Description:
------------------------------------------------------------------
$History: $
------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gl/glut.h>
typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int u_int;
typedef unsigned long u_long;
#define for if(0); else for
inline float sign(float x)
{
return (x < 0.0f)? -1.0f : 1.0f;
}
inline float frand(float x=1.0f)
{
return (rand() / (float) RAND_MAX) * x;
}
inline swapf(float& a, float& b)
{
float c = a;
a = b;
b = c;
}
inline float clampf(float x, float min, float max)
{
return (x < min)? min : (x > max)? max : x;
}
inline float wrapf(float x, float min, float max)
{
return (x < min)? (x - min) + max : (x > max)? (x - max) + min : x;
}
inline float Pi()
{
const float pi = atan(1.0f) * 4.0f;
return pi;
}
inline float TwoPi()
{
const float two_pi = 2.0f * Pi();
return two_pi;
}
inline float RadiansToDegrees(float rad)
{
const float k = 180.0f / Pi();
return rad * k;
}
inline float DegreesToRadians(float deg)
{
const float k = Pi() / 180.0f;
return deg * k;
}
//===========================================================================
// VECTORS
//===========================================================================
class Matrix;
class Vector
{
public:
float x,y;
static const Vector& Blank() { static const Vector V(0, 0); return V; }
public:
inline Vector(void)
{}
inline Vector(float Ix,float Iy)
: x(Ix)
, y(Iy)
{}
inline Vector &operator /=(const float Scalar) { x /= Scalar; y /= Scalar; return *this; }
inline Vector &operator *=(const float Scalar) { x *= Scalar; y *= Scalar; return *this; }
inline Vector &operator +=(const Vector &Other) { x += Other.x; y += Other.y; return *this; }
inline Vector &operator -=(const Vector &Other) { x -= Other.x; y -= Other.y; return *this; }
inline float operator ^ (const Vector &V) const { return (x * V.y) - (y * V.x); } // cross product
inline float operator * (const Vector &V) const { return (x*V.x) + (y*V.y); } // dot product
inline Vector operator * (float s) const { return Vector(x*s, y*s); }
inline Vector operator / (float s) const { return Vector(x/s, y/s); }
inline Vector operator + (const Vector &V) const { return Vector(x+V.x, y+V.y); }
inline Vector operator - (const Vector &V) const { return Vector(x-V.x, y-V.y); }
friend Vector operator * (float k, const Vector& V) { return Vector(V.x*k, V.y*k); }
Vector operator * (const Matrix& M) const;
Vector operator ^ (const Matrix& M) const;
Vector& operator *=(const Matrix& M);
Vector& operator ^=(const Matrix& M);
inline Vector operator -(void) const { return Vector(-x, -y); }
inline float Length(void) const { return (float) sqrt(x*x + y*y); }
float Normalise(void)
{
float fLength = Length();
if (fLength == 0.0f)
return 0.0f;
(*this) *= (1.0f / fLength);
return fLength;
}
Vector Direction(void) const
{
Vector Temp(*this);
Temp.Normalise();
return Temp;
}
float Angle(const Vector& xE)
{
float dot = (*this) * xE;
float cross = (*this) ^ xE;
// angle between segments
float angle = (float) atan2(cross, dot);
return angle;
}
Vector& Rotate(float angle)
{
float tx = x;
x = x * cos(angle) - y * sin(angle);
y = tx * sin(angle) + y * cos(angle);
return *this;
}
Vector& Rotate(const Vector& xCentre, float fAngle)
{
Vector D = *this - xCentre;
D.Rotate(fAngle);
*this = xCentre + D;
return *this;
}
void Clamp(const Vector& min, const Vector& max)
{
x = (x < min.x)? min.x : (x > max.x)? max.x : x;
y = (y < min.y)? min.y : (y > max.y)? max.y : y;
}
void Randomise(const Vector& xMin, const Vector& xMax)
{
x = frand(xMax.x - xMin.x) + xMin.x;
y = frand(xMax.y - xMin.y) + xMin.y;
}
};
#define OLI_VECTOR_H
/*
------------------------------------------------------------------
File: Vector.h
Started: 09/01/2004 22:20:40
$Header: $
$Revision: $
$Locker: $
$Date: $
Author: Olivier renault
------------------------------------------------------------------
Module:
Description:
------------------------------------------------------------------
$History: $
------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gl/glut.h>
typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int u_int;
typedef unsigned long u_long;
#define for if(0); else for
inline float sign(float x)
{
return (x < 0.0f)? -1.0f : 1.0f;
}
inline float frand(float x=1.0f)
{
return (rand() / (float) RAND_MAX) * x;
}
inline swapf(float& a, float& b)
{
float c = a;
a = b;
b = c;
}
inline float clampf(float x, float min, float max)
{
return (x < min)? min : (x > max)? max : x;
}
inline float wrapf(float x, float min, float max)
{
return (x < min)? (x - min) + max : (x > max)? (x - max) + min : x;
}
inline float Pi()
{
const float pi = atan(1.0f) * 4.0f;
return pi;
}
inline float TwoPi()
{
const float two_pi = 2.0f * Pi();
return two_pi;
}
inline float RadiansToDegrees(float rad)
{
const float k = 180.0f / Pi();
return rad * k;
}
inline float DegreesToRadians(float deg)
{
const float k = Pi() / 180.0f;
return deg * k;
}
//===========================================================================
// VECTORS
//===========================================================================
class Matrix;
class Vector
{
public:
float x,y;
static const Vector& Blank() { static const Vector V(0, 0); return V; }
public:
inline Vector(void)
{}
inline Vector(float Ix,float Iy)
: x(Ix)
, y(Iy)
{}
inline Vector &operator /=(const float Scalar) { x /= Scalar; y /= Scalar; return *this; }
inline Vector &operator *=(const float Scalar) { x *= Scalar; y *= Scalar; return *this; }
inline Vector &operator +=(const Vector &Other) { x += Other.x; y += Other.y; return *this; }
inline Vector &operator -=(const Vector &Other) { x -= Other.x; y -= Other.y; return *this; }
inline float operator ^ (const Vector &V) const { return (x * V.y) - (y * V.x); } // cross product
inline float operator * (const Vector &V) const { return (x*V.x) + (y*V.y); } // dot product
inline Vector operator * (float s) const { return Vector(x*s, y*s); }
inline Vector operator / (float s) const { return Vector(x/s, y/s); }
inline Vector operator + (const Vector &V) const { return Vector(x+V.x, y+V.y); }
inline Vector operator - (const Vector &V) const { return Vector(x-V.x, y-V.y); }
friend Vector operator * (float k, const Vector& V) { return Vector(V.x*k, V.y*k); }
Vector operator * (const Matrix& M) const;
Vector operator ^ (const Matrix& M) const;
Vector& operator *=(const Matrix& M);
Vector& operator ^=(const Matrix& M);
inline Vector operator -(void) const { return Vector(-x, -y); }
inline float Length(void) const { return (float) sqrt(x*x + y*y); }
float Normalise(void)
{
float fLength = Length();
if (fLength == 0.0f)
return 0.0f;
(*this) *= (1.0f / fLength);
return fLength;
}
Vector Direction(void) const
{
Vector Temp(*this);
Temp.Normalise();
return Temp;
}
float Angle(const Vector& xE)
{
float dot = (*this) * xE;
float cross = (*this) ^ xE;
// angle between segments
float angle = (float) atan2(cross, dot);
return angle;
}
Vector& Rotate(float angle)
{
float tx = x;
x = x * cos(angle) - y * sin(angle);
y = tx * sin(angle) + y * cos(angle);
return *this;
}
Vector& Rotate(const Vector& xCentre, float fAngle)
{
Vector D = *this - xCentre;
D.Rotate(fAngle);
*this = xCentre + D;
return *this;
}
void Clamp(const Vector& min, const Vector& max)
{
x = (x < min.x)? min.x : (x > max.x)? max.x : x;
y = (y < min.y)? min.y : (y > max.y)? max.y : y;
}
void Randomise(const Vector& xMin, const Vector& xMax)
{
x = frand(xMax.x - xMin.x) + xMin.x;
y = frand(xMax.y - xMin.y) + xMin.y;
}
};
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询