- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 49.1k
 
Create digital differential analyzer_line.py #10929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            36 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      c62f30a
              
                Create DDA_line_drawing.py
              
              
                nababuddin d08be9a
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 220559a
              
                Rename DDA_line_drawing.py to digital differential analyzer_line_draw…
              
              
                nababuddin 5d4b127
              
                Rename DDA_line_drawing.py to digital_differential_analyzer_line_draw…
              
              
                nababuddin e8a0ab7
              
                Update digital_differential_analyzer_line_drawing.py
              
              
                nababuddin 1b621e1
              
                Update digital_differential_analyzer_line_drawing.py
              
              
                nababuddin 8391c5f
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 70764bb
              
                Update digital_differential_analyzer_line_drawing.py
              
              
                nababuddin d84e4ec
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] a6b452f
              
                Update digital_differential_analyzer_line_drawing.py
              
              
                nababuddin 06f87eb
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 7f2ee3c
              
                Update digital_differential_analyzer_line_drawing.py
              
              
                nababuddin f415328
              
                Update digital_differential_analyzer_line_drawing.py
              
              
                nababuddin 5cae215
              
                Update digital_differential_analyzer_line_drawing.py
              
              
                nababuddin 5234d02
              
                Update digital_differential_analyzer_line_drawing.py
              
              
                nababuddin f34960f
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 842ad9e
              
                Apply suggestions from code review
              
              
                nababuddin 74b5dd1
              
                Update and rename digital_differential_analyzer_line_drawing.py to di…
              
              
                nababuddin dc1f4b2
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 836ce48
              
                Update digital_differential_analyzer_line.py
              
              
                nababuddin 3aa73ba
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] d9f2d33
              
                Update digital_differential_analyzer_line.py
              
              
                nababuddin a4dcebf
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 6d1844a
              
                Update digital_differential_analyzer_line.py
              
              
                nababuddin 67ca0e6
              
                Update digital_differential_analyzer_line.py
              
              
                nababuddin 94972ae
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 9287725
              
                Update digital_differential_analyzer_line.py
              
              
                nababuddin b4b89ca
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 6f0540d
              
                Update digital_differential_analyzer_line.py
              
              
                nababuddin 11e9315
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 93ed88b
              
                Update digital_differential_analyzer_line.py
              
              
                nababuddin ac7607e
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] 7ce5261
              
                Apply suggestions from code review
              
              
                tianyizheng02 8dab7f4
              
                Fix doctest
              
              
                tianyizheng02 4a91773
              
                Trigger GH workflows
              
              
                tianyizheng02 6f381df
              
                Fix function call in main block
              
              
                tianyizheng02 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import matplotlib.pyplot as plt | ||
| 
     | 
||
| 
     | 
||
| def digital_differential_analyzer_line( | ||
| p1: tuple[int, int], p2: tuple[int, int] | ||
| ) -> list[tuple[int, int]]: | ||
| """ | ||
| Draws a line between two points using the DDA algorithm. | ||
| 
     | 
||
| Args: | ||
| - p1: Coordinates of the starting point. | ||
| - p2: Coordinates of the ending point. | ||
| Returns: | ||
| - List of coordinate points that form the line. | ||
| 
     | 
||
| >>> digital_differential_analyzer_line((1, 1), (4, 4)) | ||
| [(2, 2), (3, 3), (4, 4)] | ||
| """ | ||
| x1, y1 = p1 | ||
| x2, y2 = p2 | ||
| dx = x2 - x1 | ||
| dy = y2 - y1 | ||
| steps = max(abs(dx), abs(dy)) | ||
| x_increment = dx / float(steps) | ||
| y_increment = dy / float(steps) | ||
| coordinates = [] | ||
| x: float = x1 | ||
| y: float = y1 | ||
| for _ in range(steps): | ||
| x += x_increment | ||
| y += y_increment | ||
| coordinates.append((int(round(x)), int(round(y)))) | ||
| return coordinates | ||
| 
     | 
||
| 
     | 
||
| if __name__ == "__main__": | ||
| import doctest | ||
| 
     | 
||
| doctest.testmod() | ||
| 
     | 
||
| x1 = int(input("Enter the x-coordinate of the starting point: ")) | ||
| y1 = int(input("Enter the y-coordinate of the starting point: ")) | ||
| x2 = int(input("Enter the x-coordinate of the ending point: ")) | ||
| y2 = int(input("Enter the y-coordinate of the ending point: ")) | ||
| coordinates = digital_differential_analyzer_line((x1, y1), (x2, y2)) | ||
| x_points, y_points = zip(*coordinates) | ||
| plt.plot(x_points, y_points, marker="o") | ||
| plt.title("Digital Differential Analyzer Line Drawing Algorithm") | ||
| plt.xlabel("X-axis") | ||
| plt.ylabel("Y-axis") | ||
| plt.grid() | ||
| plt.show() | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.