Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3937874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:07:09+00:00 2026-05-20T00:07:09+00:00

Given a w x h sized grid, produced in the following way self.grid =

  • 0

Given a w x h sized grid, produced in the following way

self.grid = [ ['-'] * self.w ] * self.h

I wish to “draw” a “line” of characters between two points in said grid. The following code is what I’ve come up with

def line( self, char, (x1, y1), (x2, y2) ):
    self.point( char, (x1, y1) )

    x = x1 + cmp( x2, x1 )
    y = y1 + cmp( y2, y1 )
    while x != x2 or y != y2:
        self.point( char, (x, y) )
        x = x + cmp( x2, x )
        y = y + cmp( y2, y )

    self.point( char, (x2, y2) )

Where the point() function simply fills in a single point in the grid with char.

This works like a charm for straight lines and perfect diagonals. It works for “crooked” lines too, in the sense that it doesn’t throw errors, but it doesn’t exactly look like a line between two points, more like… I don’t know, a hockey stick.

For example, given a 10×7 grid and the call

line( 'X', (1,1), (5,9) )

I get

----------
-X--------
--X-------
---X------
----X-----
-----XXXXX
----------

What I’d like is probably something more like

----------
-X--------
---X------
-----X----
-------X--
---------X
----------

How would I do this, while not breaking it for straight lines and perfect diagonals in the process? Do I need two distinct pieces of code to handle the two cases, or can one algorithm do both?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:07:10+00:00Added an answer on May 20, 2026 at 12:07 am

    You should be using Bresenham’s line algorithm, if you insist on programming this yourself at all. Furthermore, be careful about initialising the grid like this:

    grid = [ ['-'] * self.w ] * self.h
    

    because of edning up with self.h copies of the same list:

    grid = [ ['-'] * 3 ] * 3 
    grid[0][0] = 'X'
    print grid
    # [['X', '-', '-'], ['X', '-', '-'], ['X', '-', '-']]
    

    Use

    grid = [['-'] * self.w for ignored in xrange(self.h)]
    

    or Numpy arrays instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a UIImage of any dimension, I wish to generate a square icon sized
Given a sequence of data (it may have duplicates), a fixed-sized moving window, move
Is there an efficient way to create a file with a given size in
Given two sorted arrays: A and B . The size of array A is
I'm trying to solve the following problem: Given an input of, say, 0000000000000000 0011111111110000
I wish to create a 2D set of N points (typically 1e2 - 1e4)
Given an array: [dog, cat, mouse] what is the most elegant way to create:
Given the following ggplot2 chart: ggplot(my_data, aes(colour=my_factor) + geom_point(aes(x=prior, y=current)) + facet_grid(gender ~ age)
I'm trying to create a grid where the users can 'draw' across it and
I want to load an image, resize it to a given size and after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.