needle.cpp

来自「这是一个Offline工具程序」· C++ 代码 · 共 55 行

CPP
55
字号
// Needle.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"

int main(int argc, char* argv[])
{
	int radius, num, x, y, i, adjust;
	double pi = 3.1415926535, step, angle = 0;
	double result;

	if(argc != 3)
	{
		printf("Usage:	Needle.exe needle_len, dot_number\n\
				\r\tUsed to generate coordinate positions on a circle.\n\
				\r\tneedle_len: length of needle. i.e., length of the radius.\
				\r\tdot_number: dot number on the circle boundary,\n\r\
				\r\t            e.g., if used for drawing a clock, it should be 60,\n\r\
				\r\t                  if used for drawing a pie-chart, it should be 360.\n\r\
			\r\te.g., Needle 20, 60\n\n\r" );

		return 0;
	}

	radius = atoi(argv[1]);
	num = atoi(argv[2]);
	step = 2*pi/num;

	for(i = 0; i < num; i++)
	{
		if(!(i%4))
		{
			printf("\n");
		}

		result = radius * sin(angle);
		adjust = ((result - (int)result) >= 0.5)?1:0;
		x = abs( (int)result + adjust );

		result = radius * cos(angle);
		adjust = ((result - (int)result) >= 0.5)?1:0;
		y = abs( (int)result + adjust );

		printf("{%-2d, %-2d}, ", x, y);
		angle += step;
	}

	printf("\n");

	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?