Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Line Drawing Algorithm

Posted on 2003-11-26
7
Medium Priority
?
1,560 Views
Last Modified: 2010-05-18
Hi Experts!

I Want To Write a Procedure That Draws a Line In Graphic Mode 640x480.
I Know That TP7.0 Has This Procedure in Graph.tpu But Because I'm Writing My New OS With Assembly,So I Have To Write a New Proc For Drawing Line.

I Have Read Bresenham's Line Algorithm But It Has a Lot Of Problems Like:
When Y is bigger then X The Procedure Draws Just a Straight Line.

Please Help Me.
(USE PUTPIXEL IN YOUR EXAMPLE)

Bye
Programmer-x
 
0
Comment
Question by:Hamidreza Vakilian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Accepted Solution

by:
MrT-RSI earned 150 total points
ID: 9832776
 procedure line(x1,y1,x2,y2 : integer;color : byte);
  var
    x21l,y21l,d1xl,d1yl,d2xl,d2yl,max21l,min21l,duml,loopl : integer;
  begin
    x21l := x2 - x1;
    y21l := y2 - y1;
    d1xl := sign(x21l);
    d1yl := sign(y21l);
    d2xl := d1xl;
    d2yl := 0;
    max21l := abs(x21l);
    min21l := abs(y21l);
    if max21l < min21l then begin
      d2xl := 0;
      d2yl := d1yl;
      duml := max21l; max21l := min21l; min21l := duml;
    end;
    duml := max21l shr 1;
    for loopl := 0 to max21l do begin
      putpixel(x1,y1,color);
      duml := duml + min21l;
      if duml >= max21l then begin
        duml := duml - max21l;
        x1 := x1 + d1xl;
        y1 := y1 + d1yl;
      end else begin
        x1 := x1 + d2xl;
        y1 := y1 + d2yl;
      end;
    end;
  end;

with

  function sign(a : integer) : integer; external;
    { Returns the sign of value a (negative: -1, positive: 1, zero: 0).      }

in 32 bit asm:

  sign proc near a : word
    cmp a,0
    setg al ;al = 1 if a > 0
    setl ah ;ah = 1 if a < 0
    sub al,ah ;-1 if a < 0, 0 if a = 0, 1 if a > 0
    cbw ;return an integer
    ret
  sign endp

This has a own putpixel, which also includes color, just remove the color param for normal putpixel.

Good luck,

- Rob.
0
 
LVL 15

Author Comment

by:Hamidreza Vakilian
ID: 9833949
Hi MrT-RSI!

Thanks For Your Code.
Can You Convert it To Assembly?
If You Do That,I Will Give 50 Points More.

Bye
Programmer-x
0
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
LVL 12

Expert Comment

by:Hypo
ID: 9840670
I have an almost complete graphicengine/unit that you can use if you like, I wanted to use graphic
functions that were very optimized for speed in the graphicmodes I wanted to use...
so most of the routines I wrote in assembler and optimized them for mode 13 and
some other Vesa 2.0 modes,

/Hypo
0
 
LVL 2

Expert Comment

by:MrT-RSI
ID: 9842601
make that 150 and I'll do it !
0
 
LVL 15

Author Comment

by:Hamidreza Vakilian
ID: 9842889
Hi MrT-RSI!

All Right.I Have Converted It To Assembler,But It Has Some Errors When I Use it In My Program.If You Tell What I'm Doing Wrong I Will Give You That 50 Points More.(50+50=100)
**************************************************************
LINE      MACRO      X1,Y1,X2,Y2
LOCAL      CON1,CON2,OK,IF1,IF2,FIN
X21      DW      0
Y21      DW      0
D1X      DW      0
D1Y      DW      0
D2X      DW      0
D2Y      DW      0
MAX21      DW      0
MIN21      DW      0
DUM      DW      0
X      DW      X1
Y      DW      Y1
XX      DW      X2
YY      DW      Y2
PUSHA

MOV      AX,XX
SUB      AX,X
MOV      X21,AX

MOV      AX,YY
SUB      AX,Y
MOV      Y21,AX

SIGN      X21
MOV      D1X,AX

SIGN      Y21
MOV      D1Y,AX

MOV      AX,D1X
MOV      D2X,AX

MOV      D2Y,0

ABS      X21      
MOV      MAX21,AX

ABS      Y21
MOV      MIN21,AX

MOV      AX,MAX21

CMP      AX,MIN21
JL      CON1

JMP      OK

CON1:
MOV      D2X,0
MOV      AX,D1Y
MOV      D2Y,AX

MOV      AX,MAX21
MOV      DUM,AX

MOV      AX,MIN21
MOV      MAX21,AX

MOV      AX,DUM
MOV      MIN21,AX

OK:

MOV      AX,MAX21
SHR      AX,1
MOV      DUM,AX

MOV      CX,MAX21

CON2:            

PIXEL      X,Y

MOV      AX,DUM
ADD      AX,MIN21
MOV      DUM,AX

CMP      AX,MAX21
JGE      IF1
JMP      IF2

IF1:
MOV      AX,DUM
SUB      AX,MAX21
MOV      DUM,AX

MOV      AX,X
ADD      AX,D1X
MOV      X,AX

MOV      AX,Y
ADD      AX,D1Y
MOV      Y,AX

JMP      FIN

IF2:
MOV      AX,X
ADD      AX,D2X
MOV      X,AX

MOV      AX,Y
ADD      AX,D2Y
MOV      Y,AX

FIN:
LOOP      CON2      

POPA
ENDM
**************************************************************
Best Wishes...
Programmer-x
0
 
LVL 15

Author Comment

by:Hamidreza Vakilian
ID: 9845394
Hi MrT-RSI!

I Understood My Problem.I Converted It To ASM and It Works Very Good...

Best Wishes...
Programmer-x
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes Administrators rights are not enough. These cases call for the SYSTEM account. The process in this article outlines the steps required to execute commands using the SYSTEM account.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question