Ⅰ c语言画直线和填充的程序,要有算法的
c 画的指针式 Roma 表
#include <dos.h>
#include <stdio.h>
#include <math.h>
#include <graphics.h>
void drawhourbrick(int x, int y, int color, float arg);
void drawminutebrick(int x, int y, int color, float arg);
void drawsecondbrick(int x, int y, int color, float arg);
void polygon(int n, int x, int y, int r, int color, float arg, int fillstyle);
int main()
{
int GraphDriver;
int GraphMode;
float arg = 292.5;
float hourarg = 0;
float minuterarg = 0;
float secondrarg = 0;
char buffer[10];
int a, b;
int x, y, r;
int h;
int xmo[12] = {1, 2, -6, -1, -1, -2, -2, -1, 0, -4, -4, -5};
int ymo[12] = {-1, -2, -1, -1, -1, -5, 2, 1, -2, -4, -4, 0};
long delay;
float degree;
struct time time, time1;
GraphDriver = DETECT;
initgraph(&GraphDriver, &GraphMode, "");
x = 300;
y = 220;
r = 200;
degree = atan(1) / 45;
polygon(8, x, y, r, 12, arg, 0);
polygon(8, x, y, r - 2, 11, arg, 0);
polygon(8, x, y, r - 4, 14, arg, 0);
setcolor(12);
/*settextstyle(DEFAULT_FONT, 1, 0);*/
for (a = 0; a < 12; a++)
{
sprintf(buffer, "%d", a + 1);
outtextxy(x + (r - 22) * cos((a - 2) * 30 * degree) + xmo[a], y + (r - 22) * sin((a - 2) * 30 * degree) + ymo[a], buffer);
}
drawhourbrick(x, y, 14, hourarg);
drawminutebrick(x, y, 9, minuterarg);
drawsecondbrick(x, y, 13, secondrarg);
while(1)
{
gettime(&time);
if (time.ti_hour != time1.ti_hour || time.ti_min != time1.ti_min || time.ti_sec != time1.ti_sec)
{
h = time.ti_hour;
if (h > 12)
h -= 12;
drawhourbrick(x, y, 0, hourarg);
drawminutebrick(x, y, 0, minuterarg);
drawsecondbrick(x, y, 0, secondrarg);
hourarg = (h % 12) * 30 + time.ti_min * 0.5 + time.ti_sec * 0.1 / 60;
minuterarg = time.ti_min * 6 + time.ti_sec * 0.1;
secondrarg = time.ti_sec * 6;
setcolor(8);
outtextxy(x - 15, y + 120, "Roma");
drawhourbrick(x, y, 14, hourarg);
drawminutebrick(x, y, 9, minuterarg);
drawsecondbrick(x, y, 13, secondrarg);
time1 = time;
}
while(kbhit())
{
a = getch();
if (a == 27)
{
closegraph();
return 0;
}
}
}
}
void polygon(int n, int x, int y, int r, int color, float arg, int fillstyle)
{
double pi;
int i;
float x1[9], y1[9];
setcolor(color);
pi = atan(1) * 4;
arg = atan(1) / 45 * arg;
x1[1] = x + r * cos(2 * pi / n + arg);
y1[1] = y + r * sin(2 * pi / n + arg);
moveto(x1[1], y1[1]);
for (i = 2; i <= n; i++)
{
x1[i] = x + r * cos(2 * pi * i / n + arg);
y1[i] = y + r * sin(2 * pi * i / n + arg);
lineto(x1[i], y1[i]);
}
lineto(x1[1], y1[1]);
if (fillstyle != 0)
{
setfillstyle(SOLID_FILL, color);
floodfill(x, y, color);
}
}
void drawhourbrick(int x, int y, int color, float arg)
{
double pi;
int i;
float x1[4], y1[4];
setcolor(color);
pi = atan(1) / 45;
x1[0] = x;
y1[0] = y;
x1[1] = x + 20 * cos(pi * (arg - 90 - 23));
y1[1] = y + 20 * sin(pi * (arg - 90 - 23));
x1[2] = x + 25 * cos(pi * (arg - 90 + 23));
y1[2] = y + 25 * sin(pi * (arg - 90 + 23));
x1[3] = x + 120 * cos(pi * (arg - 90 + 0));
y1[3] = y + 120 * sin(pi * (arg - 90 + 0));
moveto(x1[1], y1[1]);
lineto(x1[0], y1[0]);
lineto(x1[2], y1[2]);
lineto(x1[3], y1[3]);
lineto(x1[1], y1[1]);
setfillstyle(SOLID_FILL, color);
/*
floodfill((x1[0] + x1[3]) / 2, (y1[0] + y1[3]) / 2, color);
*/
}
void drawminutebrick(int x, int y, int color, float arg)
{
double pi;
int i;
float x1[4], y1[4];
setcolor(color);
pi = atan(1) / 45;
x1[0] = x;
y1[0] = y;
x1[1] = x + 20 * cos(pi * (arg - 90 - 23));
y1[1] = y + 20 * sin(pi * (arg - 90 - 23));
x1[2] = x + 25 * cos(pi * (arg - 90 + 23));
y1[2] = y + 25 * sin(pi * (arg - 90 + 23));
x1[3] = x + 160 * cos(pi * (arg - 90 + 0));
y1[3] = y + 160 * sin(pi * (arg - 90 + 0));
moveto(x1[1], y1[1]);
lineto(x1[0], y1[0]);
lineto(x1[2], y1[2]);
lineto(x1[3], y1[3]);
lineto(x1[1], y1[1]);
setfillstyle(SOLID_FILL, color);
/*
floodfill((x1[0] + x1[3]) / 2, (y1[0] + y1[3]) / 2, color);
*/
}
void drawsecondbrick(int x, int y, int color, float arg)
{
double pi;
int i;
float x1[4], y1[4];
setcolor(color);
pi = atan(1) / 45;
x1[0] = x;
y1[0] = y;
x1[1] = x + 20 * cos(pi * (arg - 90 - 6));
y1[1] = y + 20 * sin(pi * (arg - 90 - 6));
x1[2] = x + 25 * cos(pi * (arg - 90 + 6));
y1[2] = y + 25 * sin(pi * (arg - 90 + 6));
x1[3] = x + 160 * cos(pi * (arg - 90 + 0));
y1[3] = y + 160 * sin(pi * (arg - 90 + 0));
moveto(x1[1], y1[1]);
lineto(x1[0], y1[0]);
lineto(x1[2], y1[2]);
lineto(x1[3], y1[3]);
lineto(x1[1], y1[1]);
setfillstyle(SOLID_FILL, color);
/*
floodfill((x1[0] + x1[3]) / 2, (y1[0] + y1[3]) / 2, color);
*/
}
Ⅱ C语言如何画图
c语言是函数语言,所以画图也离不开各种图形函数:下面举几个简单的例子:
=======================================
1./*学用circle画圆形*/
#include "graphics.h"
main()
{int driver,mode,i;
float j=1,k=1;
driver=VGA;mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(YELLOW);
for(i=0;i<=25;i++)
{
setcolor(8);
circle(310,250,k);
k=k+j;
j=j+0.3;
}
getch();
}
2.//line画直线
#include "graphics.h"
main()
{int driver,mode,i;
float x0,y0,y1,x1;
float j=12,k;
driver=VGA;mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(GREEN);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i<=18;i++)
{
setcolor(5);
line(x0,y0,x0,y1);
x0=x0-5;
y0=y0-5;
x1=x1+5;
y1=y1+5;
j=j+10;
}
x0=263;y1=275;y0=263;
for(i=0;i<=20;i++)
{
setcolor(5);
line(x0,y0,x0,y1);
x0=x0+5;
y0=y0+5;
y1=y1-5;
}
getch();
}
3.//用rectangle画方形
#include "graphics.h"
main()
{int x0,y0,y1,x1,driver,mode,i;
driver=VGA;mode=VGAHI;
initgraph(&driver,&mode,"");
setbkcolor(YELLOW);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i<=18;i++)
{
setcolor(1);
rectangle(x0,y0,x1,y1);
x0=x0-5;
y0=y0-5;
x1=x1+5;
y1=y1+5;
}
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(150,40,"How beautiful it is!");
line(130,60,480,60);
setcolor(2);
circle(269,269,137);
}
===================================
更多有关c语言图形方面的函数及用法请参考c语言图形学的相关知识。
Ⅲ 怎样用C语言绘制直线
1.void far line(int x0, int y0, int x1, int y1);
画一条从点(x0, y0)到(x1, y1)的直线。
2.void far lineto(int x, int y);
画一作从现行游标到点(x, y)的直线。
3.void far linerel(int dx, int dy);
画一条从现行游标(x, y)到按相对增量确定的点(x+dx, y+dy)的直线。
Ⅳ C语言像画图工具一样画直线
1)你的程序都不是一个win32程序,程序能编译通过吗?
2)你找本win32编程的书看看吧,你的绘图函数也不对,win32程序中绘图要用显示上下文环境的。
3)你这是dos编程风格和windows函数混搭,先找个例子看看再开始动手吧。
Ⅳ 怎么用C语言画一条红色的直线
首先设置初始坐标和结束坐标,然后设定画线颜色,最后用draw函数画出来就好了
Ⅵ 用VC6.0编写的画直线的C语言程序!求大神帮忙!
这个简单,用MoveTo 和LineTo就行了。如有下面2个点,x(1,2),y(8,9)
你只要MoveTo(1,2);
LineTo(8,9);
如果你是要任意输入2个点的话,那么先将输入的点先转化为浮点型,用atof函数,这个函数使用起来很简单,就是把字符型转化为浮点型。这样就好了。
Ⅶ 利用C语言编写 能够画出任意的直线算法程序(利用画点函数)
上次刚写过,在VC下运行的,
int dx,dy,incrE,incrNE,d,x,y;
if ((point[1].x-point[0].x)==0){ //垂直的直线
x=point[0].x;
for(y=point[0].y;y<point[1].y;y++)
pDC->SetPixel(x,y,50);
}
else if(abs((point[1].y-point[0].y)/(point[1].x-point[0].x))<=1){ //斜率 -1到 1 之间
dx=point[1].x-point[0].x;
dy=point[0].y-point[1].y;
d=dx-2*dy;
incrE=-2*dy;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
if(point[0].y>point[1].y){
while(x<point[1].x)
{
if(d>=0){
d+=incrE;
x++;
}
else
{d+=incrNE;
x++;
y--;
}
pDC->SetPixel(x,y,50);
}
}
else if(point[0].y<=point[1].y){
dy=point[1].y-point[0].y;
incrE=-2*dy;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
while(x<point[1].x)
{
if(d>=0){
d+=incrE;
x++;
}
else
{d+=incrNE;
x++;
y++;
}
pDC->SetPixel(x,y,50);
}
}
}
else { //斜率 <-1 和 >1的直线
if(point[1].x>=point[0].x){
dx=point[1].x-point[0].x;
dy=point[1].y-point[0].y;
d=2*dx-dy;
incrE=2*dx;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
while(x<point[1].x)
{
if(d<0){
d+=incrE;
y++;
}
else
{d+=incrNE;
pDC->SetPixel(x,y,50);
x++;
y++;
}
pDC->SetPixel(x,y,50);
}
}
else if((point[1].y-point[0].y)/(point[1].x-point[0].x)<-1){
dx=point[1].x-point[0].x;
dy=point[0].y-point[1].y;
d=2*dx-dy;
incrE=2*dx;
incrNE=2*(dx-dy);
x=point[0].x,y=point[0].y;
pDC->SetPixel(x,y,50);
while(y<point[1].y)
{
if(d>0){
d+=incrE;
y++;
}
else
{d+=incrNE;
x--;
y++;
}
pDC->SetPixel(x,y,50);
}
}
}
Ⅷ c++如何画直线
包含windows.h,里面有一个SetPixel方法,画线常用算法有三种dda,中点画线Bresenham画线->_->
刚把三种都写了下
voidCDDALineView::drawDDALine(CDC*pDC,intx0,inty0,intx1,inty1,COLORREFcolor)
{
floatdeltax,deltay,x,y;
intsteps=max(abs(x1-x0),abs(y1-y0));
deltax=(x1-x0)/steps;
deltay=(y1-y0)/steps;
x=x0;
y=y0;
pDC->SetPixel((int)(x+0.5),(int)(y+0.5),color);
for(inti=0;i<steps;i++)
{
x+=deltax;
y+=deltay;
pDC->SetPixel((int)(x+0.5),(int)(y+0.5),color);
}
}
//preCondition:x0<x1
voidCDDALineView::MidpointLine(CDC*pDC,intx0,inty0,intx1,inty1,COLORREFcolor)
{
inta=y0-y1;
intb=x1-x0;
intc=x0*y1-x1*y0;
floatd,d1,d2;
d=2*a+b;
d1=2*a;
d2=2*(a+b);
intx=x0,y=y0;
pDC->SetPixel(x,y,color);
while(x<x1)
{
if(d<0)
{
x++;
y++;
d+=d2;
}
else{
x++;
d+=d1;
}
pDC->SetPixel(x,y,color);
}
}
voidCDDALineView::BresenhamLine(CDC*pDC,intx0,inty0,intx1,inty1,COLORREFcolor)
{
intx,y,dx,dy;
dx=x1-x0;
dy=y1-y0;
floatk=dy/dx;
x=x0;
y=y0;
floate=-0.5;
for(inti=0;i<=dx;i++)
{
pDC->SetPixel(x,y,color);
x++;
e+=k;
if(e>=0)
{
y++;
e-=1;
}
}
}
仅供参考
Ⅸ C语言如何画图
framebuffer(帧缓冲)。
帧的最低数量为24(人肉眼可见)(低于24则感觉到画面不流畅)。
显卡与帧的关系:由cpu调节其数据传输速率来输出其三基色的配比。
三基色:RGB(红绿蓝)。
在没有桌面和图形文件的系统界面,可以通过C语言的编程来实现在黑色背景上画图!
用下面的代码,在需要的地方(有注释)适当修改,就能画出自己喜欢的图形!
PS:同样要编译运行后才能出效果。
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <stdlib.h>
#define RGB888(r,g,b) ((r & 0xff) <<16 | (g & 0xff) << 8 | (b & 0xff))
#define RGB565(r,g,b) ((r & 0x1f) <<11 | (g & 0x3f) << 5 | (b & 0x1f))
int main()
{
int fd = open("/dev/fb0", O_RDWR);
if(fd < 0){
perror("open err. ");
exit(EXIT_FAILURE);
printf("xres: %d ", info.xres);
printf("yres: %d ", info.yres);
printf("bits_per_pixel: %d ", info.bits_per_pixel);
size_t len = info.xres*info.yres*info.bits_per_pixel >> 3;
unsigned long* addr = NULL;
addr = mmap(NULL, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
if(addr == (void*)-1){
perror("mmap err. ");