69,371
社区成员
发帖
与我相关
我的任务
分享
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "graphics.h"
void DDA_line0(int x0,int y0,int y1,int color) {//画线函数 斜率k=无穷大
int y;
for (y=y0;y<=y1;y++) putpixel(x0,y,color);
}
void DDA_line1(int x0,int y0,int x1,int color,float k) {//画线函数 斜率|k|<=1
int x;
float y;
y=y0;
for (x=x0;x<=x1;x++) {
putpixel(x,(int)(y+0.5),color);
y=y+k;
}
}
void DDA_line2(int x0,int y0,int y1,int color,float k) {//画线函数 斜率|k|>1
int y;
float x,k1;
x=x0;
k1=1.0/k;
for (y=y0;y<=y1;y++) {
putpixel((int)(x+0.5),y,color);
x=x+k1;
}
}
void main() {
int x0=0,y0=0,x1=0,y1=0;//起点(x0,y0) 终点(x1,y1)
int gd=DETECT,gm,color=EGA_MAGENTA,errorcode,maxx,maxy;
float k=0,temp_x,temp_y;
initgraph(&gd,&gm,"D:\\BC\\BGI");
errorcode = graphresult();
if (errorcode != grOk) {
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to exit");
getch();
exit(1);
}
maxx=getmaxx();
maxy=getmaxy();
printf("Input x0 y0 x1 y1 (Note:0<=x0<x1<=%d,0<=y0<y1<=%d):",maxx,maxy);
scanf("%d %d %d %d",&x0,&y0,&x1,&y1);
if (0<=x0 && x0<=maxx
&& 0<=x1 && x1<=maxx
&& x0<x1
&& 0<=y0 && y0<=maxy
&& 0<=y1 && y1<=maxy
&& y0<y1) {
if (x0==x1) DDA_line0(x0,x1,y1,color);
else {
temp_y=y1-y0;
temp_x=x1-x0;
k=temp_y/temp_x;
if (-1<=k && k<=1) DDA_line1(x0,y0,x1,color,k);
else DDA_line2(x0,y0,y1,color,k);
}
} else {
printf("Input x0,y0,x1,y1=%d,%d,%d,%d is invalid. (Note:0<=x0<x1<=%d,0<=y0<y1<=%d)\n",x0,y0,x1,y1,maxx,maxy);
}
getch();
closegraph();
}
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
cciCursor.bVisible = FALSE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}
void ShowTheCursor() {
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
cciCursor.bVisible = TRUE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}
int main() {
HWND hwnd;
HDC hdc;
HFONT hfont;
int i,x,y;
srand(time(NULL));
system("color F0");
system("cls");
HideTheCursor();
hwnd = GetConsoleWindow();
hdc = GetDC(hwnd);
hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "华文楷体");
SelectObject(hdc,hfont);
TextOut(hdc,10,10,"地球人都知道!",14);
MoveToEx(hdc,5,5,NULL);
LineTo(hdc,300, 5);
LineTo(hdc,300, 60);
LineTo(hdc, 5, 60);
LineTo(hdc, 5, 5);
for (i=0;i<100;i++) {
x=rand()%400;
y=rand()%300;
SetPixel(hdc,x,y,0);
}
DeleteObject(hfont);
ReleaseDC(hwnd,hdc);
getchar();
system("color 07");
system("cls");
ShowTheCursor();
return 0;
}