绘图 网格 直线 正弦

/* 
 * 绘图
 * 
 * SetPiexl(HDC, int, int ,COLORREF);
 * GetPiexl(HDC, int, int);
 *
 * MoveToEx(HDC, int, int, LPPOINT);
 *
 * LineTo(HDC, int, int);                  绘制直线
 *
 * Polyline                                绘制折线
 * PolylineTo 
 *
 * GetCurrentPositionEx(HDC, LPPOINT);
 * 
 * GetClientRect(HWND, LPRECT);
 */
#include "stdafx.h"
#include "HelloWnd.h"

#include <math.h>

// 绘制网格
void DrawGrid(HDC hdc)
{
	RECT rt;
	GetClientRect(hwnd, &rt);

	for(int x=0; x < rt.right; x+=100)
	{
		MoveToEx(hdc, x, 0, NULL);
		LineTo(hdc, x, rt.bottom);
	}

	for(int y=0; y < rt.bottom; y+=100)
	{
		MoveToEx(hdc,0,y,NULL);
		LineTo(hdc, rt.right, y);
	}
}

// 绘制矩形
void DrawRectangle(HDC hdc)
{
	POINT apt[5] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100};

	MoveToEx(hdc, apt[0].x, apt[0].y, NULL);

	for(int i=1; i<5;i++)
	{
		LineTo(hdc, apt[i].x, apt[i].y);
	}
}

// 绘制折线
void DrawPolyline(HDC hdc)
{
	POINT apt[5] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100};

	Polyline(hdc, apt, 5);
}

void DrawPolylineTo(HDC hdc)
{
	POINT apt[5] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100};

	MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
	PolylineTo(hdc, apt+1, 4);
}

#define NUM   1000
#define PI    3.1415926535

// 绘制正弦波
void DrawSinewave(HDC hdc, int cx, int cy)
{
	POINT apt[NUM];

	MoveToEx(hdc, 0, cy/2, NULL);
	LineTo(hdc, cx, cy/2);

	/*
	sin(x),cos(x)等,这里的x都是弧度,而不是直接的角度。

	因此,在计算对应角度的三角函数时,需要先将角度转换成弧度再计算。

	弧度和角度的转换公式是:

	角度=弧度*180.0f/PI

	弧度=角度*PI/180.0f

	sin(30) = 1/2  
	sin(45) = sqrt(2)/2  
	sin(60) = sqrt(3)/2
	sin(90) = 1

	*/
	double b = 0;

	for(int i=0;i<NUM;++i)
	{
		b = i * PI / 180; /* 把角度转化为弧度 */

		apt[i].y = sin(b) * cy / 2 + cy / 2;
	
		apt[i].x = (cx / NUM) * i;
	}

	Polyline(hdc, apt, NUM);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值