坐标
在 EasyX 中,坐标分两种:物理坐标和逻辑坐标。
物理坐标
物理坐标是描述设备的坐标体系。
坐标原点在设备的左上角,X 轴向右为正,Y 轴向下为正,度量单位是像素。
坐标原点、坐标轴方向、缩放比例都不能改变。
逻辑坐标
逻辑坐标是在程序中用于绘图的坐标体系。
坐标默认的原点在窗口的左上角,X 轴向右为正,Y 轴向下为正,度量单位是点。
默认情况下,逻辑坐标与物理坐标是一一对应的,一个逻辑点等于一个物理像素。
以上文字段引用自《EasyX在线文档》。
下面,代码骑士将以图示的形式直观展示两种坐标的区别:
1、物理坐标(图中红点为坐标原点)
2、逻辑坐标(图中黄点)
上述示例代码:
#include<graphics.h>
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
const int width = 640;
const int height = 480;
initgraph(width, height);//640是画布宽度,480是画布高度
setlinestyle(PS_SOLID, 16);
setlinecolor(GREEN);
//y轴
line(20, 20, 20, 400);
line(0, 350, 20, 400);
line(40, 350, 20, 400);
//x轴
line(20, 20, 500, 20);
line(450, 0, 500, 20);
line(450, 40, 500, 20);
setlinecolor(RED);
fillcircle(20, 20, 10);
//字母
settextcolor(GREEN);
settextstyle(50, 0, _T("宋体"));
outtextxy(500, 50, "x");
outtextxy(50, 400, "y");
IMAGE picture;
loadimage(&picture, "屏幕截图 2021-12-25 112017.jpg", 200, 100);
int x = 200, y = 200;
setlinestyle(PS_SOLID, 5);
setlinecolor(YELLOW);
line(x, y, x, 30);
line(x, y, 30, y);
setlinestyle(PS_SOLID, 16);
putimage(x, y, &picture);
fillcircle(x, y, 10);
setbkmode(TRANSPARENT);//显示透明文字
TCHAR loaction[50];
_stprintf_s(loaction, _T("(x = %d,y = %d)"), x, y);
settextstyle(30, 0, _T("宋体"));
outtextxy(x-100, y-50, loaction);
_getch();//暂停,等待键盘按键
closegraph();//关闭当前画布
return 0;
}