Easy2d 文档教程之基础类型 上

Point 坐标点

Point 类表示一个二维坐标系中的点,具有 xy 两个属性。

类定义

class Point
	{
	public:
		float x;	// X 坐标
		float y;	// Y 坐标

	public:
		Point();

		Point(float x, float y);

		Point(const Point& other);

		Point operator + (Point const& point) const;
		Point operator - (Point const& point) const;
		Point operator * (float const& point) const;
		Point operator / (float const& point) const;
		Point operator - () const;
		bool operator== (const Point& point) const;

		operator easy2d::Size() const;

		// 判断两点间距离
		static float distance(const Point&, const Point&);
	};

由上可知,Point类型可以进行基本的运算和赋值,还可以判断两点距离。

定义与赋值

使用Point关键字定义一个Point
用法:

Point point;//定义一个Point类型的坐标点point
point.x = 10;//设定坐标x为10
point.y = 10;//设定坐标y为10

Point point_1;
point_1.x = 20;
point_1.y = 20;

以上代码也可以写成

Point point = Point(10, 10);
Point point_1 = Point(20, 20);

运算

Point类型的运算讲究点点对应。
因此有下代码

Point point = Point(10, 10);
Point point_1 = Point(20, 20);
Point point_2;
point_2 = point_1 - point;//point_2等于(10,10)
point_2 = point_1 + point;//point_2等于(30,30)
point_2 = point_1 * point;//point_2等于(200,200)
point_2 = point_1 / point;//point_2等于(2,2)
bool flag = point == point_1;//检测point是否等于point_1,值为false

第四行:相减得(10,10)
第五行:相加得(30,30)
第六行:相乘得(200,200)
第七行:相除得(2,2)
第八行:检测point是否等于point_1,值为false

distance 距离

使用distance获取坐标点的距离
用法:

Point::distance(point, point_1);

以上代码展现了distance的用法

Size 大小

Size 类表示一个二维坐标系中的物体大小,具有 width 和 height 两个属性。

类定义

class Size
	{
	public:
		float width;	// 宽度
		float height;	// 高度

	public:
		Size();

		Size(float width, float height);

		Size(const Size& other);

		Size operator + (Size const& size) const;
		Size operator - (Size const& size) const;
		Size operator * (float const& size) const;
		Size operator / (float const& size) const;
		Size operator - () const;
		bool operator== (const Size& size) const;

		operator easy2d::Point() const;
	};

定义与赋值

Point类似,使用如下命令定义。

Size size = Size(100, 100);//注意Size不像Point不能为负
Size size = Size(50, 50);

运算

Size类型的运算讲究点点对应。
因此有下代码

Size size = Size(10, 10);
Size size_1 = Size(20, 20);
Size size_2;
size_2 = size_1 - size ;//size_2等于(10,10)
size_2 = size_1 + size ;//size_2等于(30,30)
size_2 = size_1 * size ;//size_2等于(200,200)
size_2 = size_1 / size ;//size_2等于(2,2)
bool flag = size == size_1;//检测size是否等于size_1,值为false

第四行:相减得(10,10)
第五行:相加得(30,30)
第六行:相乘得(200,200)
第七行:相除得(2,2)
第八行:检测size是否等于size_1,值为false

Rect 矩阵

这个部分还未完善,如果有懂得同学请留言

class Size
	{
	public:
		float width;	// 宽度
		float height;	// 高度

	public:
		Size();

		Size(float width, float height);

		Size(const Size& other);

		Size operator + (Size const& size) const;
		Size operator - (Size const& size) const;
		Size operator * (float const& size) const;
		Size operator / (float const& size) const;
		Size operator - () const;
		bool operator== (const Size& size) const;

		operator easy2d::Point() const;
	};


	// 矩形
	class Rect
	{
	public:
		Point origin;	// 原点坐标
		Size  size;		// 宽度和高度

	public:
		Rect();

		Rect(float x, float y, float width, float height);

		Rect(const Point& pos, const Size& size);

		Rect(const Rect& other);

		Rect& operator= (const Rect& other);

		bool operator== (const Rect& rect) const;

		// 设置矩形
		void setRect(	
			float x,
			float y,
			float width,
			float height
		);

		// 判断点是否在矩形内
		bool containsPoint(
			const Point& point
		) const;

		// 判断两矩形是否相交
		bool intersects(
			const Rect& rect
		) const;

		inline Vector2 getCenter() const { return Vector2{ origin.x + size.width / 2, origin.y + size.height / 2 }; }

		inline Vector2 getLeftTop() const { return origin; }

		inline Vector2 getRightBottom() const { return Vector2{ getRight(), getBottom() }; }

		inline Vector2 getRightTop() const { return Vector2{ getRight(), getTop() }; }

		inline Vector2 getLeftBottom() const { return Vector2{ getLeft(), getBottom() }; }

		inline float getLeft() const { return origin.x; }

		inline float getTop() const { return origin.y; }

		inline float getRight() const { return origin.x + size.width; }

		inline float getBottom() const { return origin.y + size.height; }
	};


	// 二维变换矩阵
	template <typename _Lty, typename _Rty>
	struct MatrixMultiply;

	struct Matrix32
	{
		union
		{
			struct
			{
				float m[6];  // m[3][2]
			};

			struct
			{
				float
					_11, _12,
					_21, _22,
					_31, _32;
			};
		};

		Matrix32();

		Matrix32(float _11, float _12, float _21, float _22, float _31, float _32);

		Matrix32(Matrix32 const& other);

		template <typename T>
		Matrix32(T const& other)
		{
			for (int i = 0; i < 6; i++)
				m[i] = other[i];
		}

		template <typename _Lty, typename _Rty>
		inline Matrix32& operator= (MatrixMultiply<_Lty, _Rty> const& other)
		{
			Matrix32 result(other);
			(*this) = result;
			return *this;
		}

		float operator [](unsigned int index) const;


		void identity();

		Vector2 transform(const Vector2& v) const;

		Rect transform(const Rect& rect) const;

		void translate(float x, float y);

		void translate(const Vector2& v);

		float determinant() const;

		bool isIdentity() const;

		bool isInvertible() const;

		D2D1::Matrix3x2F const& toD2DMatrix() const;

		static Matrix32 translation(
			float x,
			float y);

		static Matrix32 scaling(
			float x,
			float y,
			const Point& center = Point());

		static Matrix32 rotation(
			float angle,
			const Point& center = Point());

		static Matrix32 skewing(
			float angle_x,
			float angle_y,
			const Point& center = Point());

		static Matrix32 invert(Matrix32 const& matrix);
	};

String 字符串

String 类是储存字符串内容的容器,是 std::wstring 类型的别名。
它曾经是 Easy2D 中实现的一种同时支持 charwchar_t 的字符串类型,后来为了程序的兼容性,Easy2D 抛弃了这种方式,而选择 C++ 标准库提供的 std::wstring

因为 String 只是简单的给 std::wstring 起了个别名,所以有关 String 的用法你都可以在各大学习网站上找到。

字符串以L开头

C++中以双引号括起来的字符串类型为 const char*,其中的字符都是 ASCII 码;以 L 开头的字符串类型为 const wchar_t*,其中的字符都是 Unicode 字符;在 Windows 编程中,推荐在代码中使用 Unicode 字符。

std::wstring 和 std::string

std::wstringstd::stringUnicode 版本,其中 w 的意思是 wide(宽),因为其储存的 wchar_t 是宽字符类型;使用时需要注意的地方有,将 std::cout、std::cin、std::to_string 等相关函数替换为 std::wcout、std::wcin、std::to_wstring 的宽字符版本。

// 字符串
using String = std::wstring;

// 窄字符串
using ByteString = std::string;

以上代码展示了StringByteString的定义

Color 颜色

类定义

// 颜色
class Color
{
public:
	Color();

	Color(
		float r,
		float g,
		float b
	);

	Color(
		float r,
		float g,
		float b,
		float alpha
	);

	Color(
		UINT rgb
	);

	Color(
		UINT rgb,
		float alpha
	);

	D2D1_COLOR_F toD2DColorF() const;

public:
	enum Value : UINT
	{
		Black = 0x000000,
		Blue = 0x0000FF,
		BlueViolet = 0x8A2BE2,
		Brown = 0xA52A2A,
		Chocolate = 0xD2691E,
		DarkBlue = 0x00008B,
		DarkGray = 0xA9A9A9,
		DarkGreen = 0x006400,
		DarkOrange = 0xFF8C00,
		DarkRed = 0x8B0000,
		DarkViolet = 0x9400D3,
		ForestGreen = 0x228B22,
		Gold = 0xFFD700,
		Gray = 0x808080,
		Green = 0x008000,
		GreenYellow = 0xADFF2F,
		LightBlue = 0xADD8E6,
		LightCyan = 0xE0FFFF,
		LightGreen = 0x90EE90,
		LightGray = 0xD3D3D3,
		LightPink = 0xFFB6C1,
		LightSeaGreen = 0x20B2AA,
		LightSkyBlue = 0x87CEFA,
		LightYellow = 0xFFFFE0,
		Orange = 0xFFA500,
		OrangeRed = 0xFF4500,
		Pink = 0xFFC0CB,
		Purple = 0x800080,
		Red = 0xFF0000,
		Silver = 0xC0C0C0,
		SkyBlue = 0x87CEEB,
		Snow = 0xFFFAFA,
		Violet = 0xEE82EE,
		Wheat = 0xF5DEB3,
		White = 0xFFFFFF,
		WhiteSmoke = 0xF5F5F5,
		Wood = 0xDEB887,
		Yellow = 0xFFFF00,
		YellowGreen = 0x9ACD32
	};

private:
	void _init(
		UINT rgb,
		float alpha
	);

private:
	float r;
	float g;
	float b;
	float a;
};


分支形式

Color一共有四种形式:
分别是

Color(
	float r,
	float g,
	float b
);

Color(
	float r,
	float g,
	float b,
	float alpha
);

Color(
	UINT rgb
);

Color(
	UINT rgb,
	float alpha
);

其中UINT的值可以是(格式:单元 = 16进制码)

Black = 0x000000,
Blue = 0x0000FF,
BlueViolet = 0x8A2BE2,
Brown = 0xA52A2A,
Chocolate = 0xD2691E,
DarkBlue = 0x00008B,
DarkGray = 0xA9A9A9,
DarkGreen = 0x006400,
DarkOrange = 0xFF8C00,
DarkRed = 0x8B0000,
DarkViolet = 0x9400D3,
ForestGreen = 0x228B22,
Gold = 0xFFD700,
Gray = 0x808080,
Green = 0x008000,
GreenYellow = 0xADFF2F,
LightBlue = 0xADD8E6,
LightCyan = 0xE0FFFF,
LightGreen = 0x90EE90,
LightGray = 0xD3D3D3,
LightPink = 0xFFB6C1,
LightSeaGreen = 0x20B2AA,
LightSkyBlue = 0x87CEFA,
LightYellow = 0xFFFFE0,
Orange = 0xFFA500,
OrangeRed = 0xFF4500,
Pink = 0xFFC0CB,
Purple = 0x800080,
Red = 0xFF0000,
Silver = 0xC0C0C0,
SkyBlue = 0x87CEEB,
Snow = 0xFFFAFA,
Violet = 0xEE82EE,
Wheat = 0xF5DEB3,
White = 0xFFFFFF,
WhiteSmoke = 0xF5F5F5,
Wood = 0xDEB887,
Yellow = 0xFFFF00,
YellowGreen = 0x9ACD32

记住,使用的时候应该如下代码格式书写。

Color::White        // 白色
Color::Black        // 黑色
Color::Blue         // 蓝色
Color::Brown        // 棕色
Color::Gray         // 灰色
Color::Green        // 绿色
Color::Red          // 红色
Color::Pink         // 粉色
Color::Orange       // 橘黄色
Color::OrangeRed    // 橘红色
Color::Purple       // 紫色
Color::Yellow       // 黄色

Color::Chocolate    // 巧克力色
Color::Gold         // 金色

Color::LightBlue    // 淡蓝色
Color::LightGray    // 淡灰色
Color::LightGreen   // 淡绿色

Color::DarkBlue     // 深蓝色
Color::DarkGray     // 深灰色
Color::DarkGreen    // 深绿色

// 还有更多,不再列出

构造新 Color

指定 RGB 三原色的程度来构造颜色
// 构造一个纯红色
Color red = Color(1.0, 0.0, 0.0);

颜色为红,其圆括号中三个小数分别为r, g, b

alpha 通道
// 构造一个纯红色,且透明度为 0.7
Color red = Color(1.0, 0.0, 0.0, 0.7);

颜色为带有0.7透明的红,其圆括号中三个小数分别为r, g, b, alpha 通道
alpha 通道相当于透明度。

十六进制的RGB颜色值
Color red = Color(0xFF0000);

为纯红色,同Color red = Color(1.0, 0.0, 0.0);

UNIT值
Color red = Color(Color::Red);//红色
Color blue = Color(Color::Blue);//蓝色

结束

本章只是基础类型的上章,关于所有的类型,原作者并未完全写明再Easy2d的官方网站,所以恳请大佬们帮忙补充好文档,提及贵见。
在基础类型的下章,我们将会整合KeyCode, MouseCode, Image, Font, Listener, Function几类类型,
其中KeyCode, MouseCode涉及Input功能,初学者建议有针对性的去翻阅我编写的文档教程,在了解一章的内容之后,循序渐进的去了解更多实现的必要。而不是一章一章的翻阅,会显得有些吃力。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值