Skip to main content
7 events
when toggle format what by license comment
Nov 15, 2018 at 21:08 comment added VisualMelon @benj2240 err... only now that I look at the OP code again, it is clearly broken, and fails to perform this task (please do write an answer to that effect)
Nov 15, 2018 at 20:59 comment added VisualMelon @benj2240 maybe I'm missing something, but the idea isn't to stop it throwing an IndexOutOfRange; rather, it's to stop it going from x=0, y=1 to x=width-1, y=0 (wrapping around) when you subtract 1 from the index. You are definitely right that _ty is redundant.
Nov 15, 2018 at 20:50 comment added benj2240 If targetIndex is non-negative, then targetIndex%width will also be non-negative, and so will targetIndex/width (provided width is non-negative). targetIndex%width will never be greater than or equal to width. And the only way targetIndex/width can be greater than or equal to height is if targetIndex is greater than or equal to width*height. This isn't super wasteful code, mind you. But doing the algebra at design time is more efficient than doing it at run time.
Nov 15, 2018 at 20:42 comment added VisualMelon @benj2240 they are necessary: it's checking bounds for a 2D grid (i.e. disallow wrap-around).
Nov 15, 2018 at 20:07 comment added benj2240 Not worth a full answer, but I thought I'd add on that _tx and _ty are (besides being cryptically named) not needed. You can instead just ensure that targetIndex is non-negative and less than width*height. You could even precalculate the maximum target index outside of the loop, since it will not change.
Nov 15, 2018 at 12:47 history edited VisualMelon CC BY-SA 4.0
fixes
Nov 15, 2018 at 12:35 history answered VisualMelon CC BY-SA 4.0