當前位置:首頁 » 編程語言 » c語言繪直線
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言繪直線

發布時間: 2023-03-06 08:54:06

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. ");