簡體   English   中英

澄清C圖形庫的框架代碼

[英]Clarification on skeleton code for C graphics library

我正在研究C,並且得到了這段繪制一行像素的代碼:

void draw_line(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2) {
// Insert algorithm here.
if (x1 == x2) {
    //Draw Horizontal line
    unsigned char i;
    for (i = y1; i <= y2; i++){
        set_pixel(x1, i, 1);
    }           
} else if (y1 == y2){
    //Draw vertical line
    unsigned char i;
    for (i = x1; i <= x2; i++){
        set_pixel(i, y1, 1);
    }       

我了解它是如何工作的,但不知道如何實現它。 有人可以提供一個使用示例嗎?

希望這個能對您有所幫助:

算法:
1)獲取直線起點和終點的X和Y坐標。
2)在X和Y坐標值中找到差異dx和dy。
3)檢查dx是否大於dy,然后將更大的值分配給“ steps”。
4)通過將相應的軸差逐步除以規則的間隔增加X和Y值。
5)使用“ PUTPIXEL”語法繪制初始點。
6)重復步驟4的“步驟”次數,並使用“ PUTPIXEL”語法標記終點。
7)結束程序。

Program:
#include"graphics.h"
#include"stdio.h"
#include"conio.h"
#include"math.h"
void linedraw(int,int,int,int);
int main()
{
int x1coeff,y1coeff,x2coeff,y2coeff;
printf("\Enter the x and y value for starting point:");
scanf("%d%d",&x1coeff,&y1coeff);
printf("\Enter the x and y value for end point:");
scanf("%d%d",&x2coeff,&y2coeff);
linedraw(x1coeff,y1coeff,x2coeff,y2coeff);
getch();
return 0;
}
void linedraw(int xa,int ya,int xb,int yb)
{
int dx,dy,steps,k;
int gdriver=DETECT,gmode;
float xinc,yinc,x,y;
initgraph(&gdriver,&gmode,""); //initialise graphics
dx=xb-xa;
dy=yb-ya;
x=xa;
y=ya;
if(abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xinc=dx/steps;
yinc=dy/steps;
putpixel(x,y,WHITE);
for(k=0;k {
x+=xinc;
y+=yinc;
putpixel(x,y,WHITE);
}}
Output:
Enter the x and y value for starting point:100 100
Enter the x and y value for end point:200 200
The Line drawn is

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM