
void circ(int x,int y,int r,int c)
{
int i,ty,tx
for(i=xi<=x+r++i)
{
ty=sqrt(pow(r,2)-pow((i-x),2))+y
putpixel(i,ty,(i+ty)%c)
putpixel(i,2*y-ty,(i+ty)%c)
putpixel(2*x-i,ty,(i+ty)%c)
putpixel(2*x-i,2*y-ty,(i+ty)%c)
}
for(i=yi<=y+r++i)
{
tx=sqrt(pow(r,2)-pow((i-y),2))+x
putpixel(tx,i,(tx+i)%c)
putpixel(tx,2*y-i,(tx+i)%c)
putpixel(2*x-tx,i,(tx+i)%c)
putpixel(2*x-tx,2*y-i,(tx+i)%c)
}
}
四个变量分别是圆心坐标 xy,半径,颜色
原理是先画四分之一个圆,然后用左右上下对称画全
tx=sqrt(pow(r,2)-pow((i-y),2))+x 也就是圆标准方程的变形
附上一个画圆的程序wintc1.91图形模板编写的
/* WIN-TC BGI 图形编程模板 */
#include "Conio.h"
#include "graphics.h"
#include"stdlib.h"
#include"time.h"
#include"math.h"
#define closegr closegraph
void initgr(void) /* BGI初始化 */
{
int gd = DETECT, gm = 0/* 和gd = VGA,gm = VGAHI是同样效果 */
registerbgidriver(EGAVGA_driver)/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */
initgraph(&gd, &gm, "")
}
int main(void)
{
int i,k,x,y,rr
void circ(int,int,int,int)
int pr()
initgr()/* BGI初始化 */
srand(time(NULL))
for(k=0k<50++k)
{
rr=rand()%150+50
x=(rand()%2)*2-1
y=(rand()%2)*2-1
for(i=1i<600++i)
circ(320+rr*x,240+rr*y,i,k+2)
}
getch()/* 暂停一下,看看前面绘图代码的运行结果 */
closegr()/* 恢复TEXT屏幕模式 */
return 0
}
int pr()
{
static int m=2
if(m>=9) m=2
return m++
}
void circ(int x,int y,int r,int c)
{
int i,ty,tx
for(i=xi<=x+r++i)
{
ty=sqrt(pow(r,2)-pow((i-x),2))+y
putpixel(i,ty,(i+ty)%c)
putpixel(i,2*y-ty,(i+ty)%c)
putpixel(2*x-i,ty,(i+ty)%c)
putpixel(2*x-i,2*y-ty,(i+ty)%c)
}
for(i=yi<=y+r++i)
{
tx=sqrt(pow(r,2)-pow((i-y),2))+x
putpixel(tx,i,(tx+i)%c)
putpixel(tx,2*y-i,(tx+i)%c)
putpixel(2*x-tx,i,(tx+i)%c)
putpixel(2*x-tx,2*y-i,(tx+i)%c)
}
}
说得还不够详细?下面是详细步骤:1、启动VC6,选择“文件-新建”,d出“新建”对话框(此时处于“工程”选项卡)。
2、在左边选择“Win32 Application”(倒数第4项),在右边输入工程名,点击“确定”。然后在d出的对话框中选择“空工程”,再点“完成”。
3、选择“文件-新建”,d出“新建”对话框(此时处于“文件”选项卡)。
4、在左边选择“C++ Source File”(第4项),在右边输入文件名,点击“确定”。
5、把下面的代码粘贴进去,然后组建、运行。
#include <windows.h>
#include <math.h>
//窗口函数
LRESULT CALLBACK WinMyProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
HDC hDC
PAINTSTRUCT ps
hDC = BeginPaint(hWnd, &ps)
//画横坐标
MoveToEx(hDC, 50, 300, NULL)
LineTo(hDC, 550, 300)
//画纵坐标
MoveToEx(hDC, 300, 50, NULL)
LineTo(hDC, 300, 550)
//设置绘制背景为透明,防止*号遮挡坐标轴
SetBkMode(hDC, TRANSPARENT)
int x, y, i
for (i=12i<=68i++)
{
x = 100 + i*5
//画上面1/4圆
y = -(int)(sqrt(40000-(x-300)*(x-300))) + 300
TextOut(hDC, x, y, "*", strlen("*"))
//画下面1/4圆
y = (int)(sqrt(40000-(x-300)*(x-300))) + 300
TextOut(hDC, x, y, "*", strlen("*"))
}
for (i=12i<=68i++)
{
y = 100 + i*5
//画左边1/4圆
x = -(int)(sqrt(40000-(y-300)*(y-300))) + 300
TextOut(hDC, x, y, "*", strlen("*"))
//画右边1/4圆
x = (int)(sqrt(40000-(y-300)*(y-300))) + 300
TextOut(hDC, x, y, "*", strlen("*"))
}
EndPaint(hWnd, &ps)
break
case WM_DESTROY:
PostQuitMessage(0)
break
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam)
}
return 0
}
//主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//设计并注册窗口类
WNDCLASS wndcls
wndcls.cbClsExtra = 0
wndcls.cbWndExtra = 0
wndcls.hbrBackground = (HBRUSH)COLOR_WINDOW
wndcls.hCursor = LoadCursor(NULL, IDC_ARROW)
wndcls.hIcon = LoadIcon(NULL, IDI_WINLOGO)
wndcls.hInstance = hInstance
wndcls.lpfnWndProc = WinMyProc
wndcls.lpszClassName = "MyWinClass"
wndcls.lpszMenuName = NULL
wndcls.style = CS_HREDRAW | CS_VREDRAW
RegisterClass(&wndcls)
//创建窗口
HWND hWnd
hWnd = CreateWindow("MyWinClass", "在屏幕上画圆", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 600, NULL, NULL, hInstance, NULL)
//显示窗口
ShowWindow(hWnd, SW_SHOWNORMAL)
UpdateWindow(hWnd)
//消息循环
MSG msg
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg)
DispatchMessage(&msg)
}
return 0
}
Option ExplicitPrivate Sub Command1_Click()
Dim CenterX As Single, CenterY As Single
Dim R As Integer, X As Single, Y As Single, i As Integer
Const pi = 3.1415926
Randomize
R = 2000
CenterX = Me.ScaleWidth / 2: CenterY = Me.ScaleHeight / 2
For i = 0 To 355 Step 5
X = CenterX + R * Cos(i * pi / 180)
Y = CenterY - R * Sin(i * pi / 180)
Me.ForeColor = QBColor(Int(Rnd() * 15))
Me.CurrentX = X: Me.CurrentY = Y
Me.Print "*"
Next
End Sub
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)