easyx带笔锋的涂鸦板!
网上有人用easyx做的涂鸦板,大部分有以下几个问题:
- 笔记不连贯,速度快会断开
- 笔记粗细相同
这次,我写了一个新的涂鸦板,能够完美解决以上问题:
- 解决笔记不连贯:记录上一次的落笔位置,连接前后两点
- 解决笔记粗细相同:记录上一次的落笔位置,计算前后两点的距离。距离越长,书写速度越快;距离越短,书写速度越慢。根据书写速度越快的不同,计算笔记的宽度,从而形成笔锋。
话不多说,直接上代码
#include <graphics.h>
#include <conio.h>
#include <cmath>
#define key_down(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) //检测按键按下
double dis(int s1x, int s1y, int s2x, int s2y) { //两点之间的距离
double a, b, c;
a = abs(s1x - s2x);
b = abs(s1y - s2y);
c = sqrt(a * a + b * b);
return c;
}
MOUSEMSG m = GetMouseMsg(); //鼠标消息
int x = m.x, y = m.y; //上一次鼠标位置
int lastc = 1; //上一次两点距离
bool keydown = false; //上一次鼠标按下
int main()
{
initgraph(860, 640);
setbkcolor(RGB(50, 50, 50));
cleardevice(); //初始化窗口
while (1)
{
m = GetMouseMsg(); //获取一条鼠标消息
if (key_down(VK_LBUTTON)) //鼠标点击
{
if (!keydown)x = m.x, y = m.y, keydown = true;
//根据动笔速度确定笔锋
if (dis(x, y, m.x, m.y) > lastc)lastc++;
else lastc--;
if (lastc < 1)lastc = 1;
if (lastc > 25)lastc = 25;
//绘制
setlinestyle(PS_SOLID, lastc);
setcolor(CYAN);
line(m.x, m.y, x, y);
x = m.x, y = m.y;
} else keydown = false;
}
return 0;
}
运行效果
是不是很不错,喜欢就点个赞吧!