• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Converting a two dimensional int array into a byte array

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at these lines from the code you posted:

There are a couple of things wrong here. First of all, notice how I formatted the code. I did this because a for loop only repeats the first line immediately afterwards. When I'm coding, I typically indent the line that is being repeated. I doubt this is what you intend. If you want the loop to repeat multiple lines, you should use { } to surround them. In fact, it is a good practice to use { } even if you only want to repeat a single line of code. That way you can easily add more lines of code without breaking it.

However, there slightly easier way to do this. In particular, I see that i already counts from xStart to xEnd. If you do it right, you can use i in the loop to show you which element in the array to access. If you use a little ingenuity, you can also use i to give you the y coordinate in the array as well. As a hint, what if i starts at 0. What do you want to count up to (or down to) in the for loop? Also, is there a way to make the code "calculate" the direction to move rather than using an if statement? If it helps, think about what the slope of the line means? In other words, how do you calculate slope and how can you use that information?

I hope this helps give you some ideas. Good luck.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
Two things to consider...

First, the conventional definition of slope might lead you astray in this context. Remember, we're not using a typical x-y system. Instead, the y values are reversed -- meaning that as y increases, the point moves downward rather than upward.

Second, as we move from xStart to xEnd, we are not necessarily increasing, because we don't know in which order the user entered the points. For example, consider drawLine(9, 9, 2, 2). Therefore, a for loop constructed as (int i = xStart; i < xEnd; i++) could be a very dangerous thing.



From my point of view, the direction that is defined for the y-values is a technical detail. The mathematics works exactly the same way. For example, if the slope is positive, it means that a positive change in x corresponds to a positive change in y. The only difference is how the "positive direction" is defined. If you are careful how you write the code, this difference will be invisible in the calculations. The only place it shows up is how you access the elements in the array.

Layne
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Layne Lund:
...From my point of view, the direction that is defined for the y-values is a technical detail. The mathematics works exactly the same way...


True, the mathematical definition is exactly the same. But this is a "technical detail" that can be very confusing in writing the code if you are envisioning this backwards (or upside down, as the case may be).
[ September 23, 2005: Message edited by: marc weber ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys I'll be honest with you. This is due tonight and I don't know how to complete it with what has been said so far. I'm just going to have to turn it in as is and hope I don't fail it completely.

Thanks for the help, everyone.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's not too late, I'll toss out this last suggestion.

I found it helpful to identify which x and y values were greater -- especially since we don't know the order in which coordinates might have been entered. This allowed me to set starting and ending points for my loops, knowing whether I needed to increment or decrement between them.

There are more elegant and efficient ways to do this, but I think this is a good approach to keep track of what's going on.
[ September 23, 2005: Message edited by: marc weber ]
 
It's feeding time! Give me the food you were going to give to this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic