easyx下繪製心形圖案
在網上找了好久一直沒找到,在easyx下畫心形的代碼,一搜全是在C下的printf圖案,轉都不好轉爲easyx的工程,實在不能忍,只好去找找心形曲線的函數,自己動手豐衣食足,修修改改弄了好久,終於達到了較爲滿意的效果,還是蠻像的,效果如下
這只是基礎的,爲後續心形圖案的應用做點前言
完整代碼如下:
////////////////////////////////////////////
// 程序名稱:心形圖案
// 編譯環境:Visual Studio 2013,EasyX 2017-9-19
// 程序編寫:鼠瓜
// 最後更新:2018-12-11
//
#include <graphics.h>
#include <conio.h>
#include <math.h>
//繪心形函數
//x0,y0,位置
//size:大小
//COLORREF C:顏色
void heart(int x0, int y0, int size, COLORREF C)
{
double m, n, x, y; double i;
for (i = 0; i <= 2*size; i = i + 0.01)
{
//產生極座標點
m = i;
n = -size * (((sin(i)*sqrt(fabs(cos(i)))) / (sin(i) + 1.4142)) - 2 * sin(i) + 2);
//轉換爲笛卡爾座標
x = n*cos(m) + x0;
y = n*sin(m) + y0;
putpixel(x, y, C);
}
}
void main()
{
initgraph(640, 480);
heart(300,150,80 ,RED);
_getch();
closegraph();
}