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 6329473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:40:21+00:00 2026-05-24T17:40:21+00:00

I had the following code of bresenham’s algorithm which represents adapted to Scala Java

  • 0

I had the following code of bresenham’s algorithm which represents adapted to Scala Java code.

def bresenham(x0: Int, y0: Int, x1: Int, y1: Int) = {
    import scala.math.abs

    val dx = abs(x1 - x0)
    val dy = abs(y1 - y0)

    val sx = if (x0 < x1) 1 else -1
    val sy = if (y0 < y1) 1 else -1

    new Iterator[(Int, Int)] {
      var (x, y) = (x0, y0)
      var err = dx - dy

      def next = {
        val omitted = (x, y)
        val e2 = 2 * err
        if (e2 > -dy) {
          err -= dy
          x += sx
        }
        if (e2 < dx) {
          err += dx
          y += sy
        }
        omitted
      }

      def hasNext = (x <= x1 && y <= y1)
    }
  }

For almost all lines all goes fine, but when I’m trying to compute vertical lines from top to bottom (i.e. (0,3) -> (0,0) ) I’m getting nothing.
I feel stupid about myself, because problem is not so hard and lies in hasNext which says nope for a case above).
I’ve dealt with that by swapping points, but that’s obviously a bad solution.
Can anybody help me to generalize algorithm?

  • 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-24T17:40:23+00:00Added an answer on May 24, 2026 at 5:40 pm

    In the failure case, you’re trying to go from y0 = 3 to y1 = 0. So the step will be negative, sy = -1. The condition for continuing in hasNext should then depend on y >= y1 rather than what you wrote (y <= y1).

    hasNext must be generalized to handle either direction. A clever way would be,

    def hasNext = (sx*x <= sx*x1 && sy*y <= sy*y1)
    

    which works because sx and sy are nonzero, and their sign determines the direction of the steps.

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

Sidebar

Related Questions

I had the following code to generate a hash of an object: public int
Say I had the following code: % Cellmode_subfunction_test.m %% Cell 1 foo(1); %% Cell
I had the following line snippet of code that searches for a propery of
What would be the best hashing algorithm if we had the following priorities (in
I had the following code suggested to me. My question is to ask if
If I had the following code: set alpha=abcdefghijklmnopqrstuvwxyz What would I have to do
If I had the following select, and did not know the value to use
A long time ago I had the following directory structure in my SVN repository
Conceptually, I would like to accomplish the following but have had trouble understand how
Conceptually, I would like to accomplish the following but have had trouble understand how

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.