Skip to content

Instantly share code, notes, and snippets.

@PadraigK
Last active August 29, 2015 14:02
Show Gist options
  • Save PadraigK/d6d64787510136558552 to your computer and use it in GitHub Desktop.
Save PadraigK/d6d64787510136558552 to your computer and use it in GitHub Desktop.
Help me get better at Swift
func rangeOfPixelsForRow(row: Int) -> Range<Int>
{
return row * imageSize.width..(row+1)*imageSize.width
}
func rangeOfPixelsForColumn(column: Int) -> StridedRangeGenerator<Int>
{
return (column..imageSize.height*imageSize.width).by(imageSize.width)
}
func indicesOfEdgePixels(inset: Int) -> Array<Int>
{
var indices = Int[]()
for pixelIndex in rangeOfPixelsForRow(0)
{
indices.append(pixelIndex)
}
for pixelIndex in rangeOfPixelsForRow(imageSize.height-1)
{
indices.append(pixelIndex)
}
for pixelIndex in rangeOfPixelsForColumn(0)
{
indices.append(pixelIndex)
}
for pixelIndex in rangeOfPixelsForColumn(imageSize.width-1)
{
indices.append(pixelIndex)
}
return indices
}
@PadraigK
Copy link
Author

There's currently a bug where this code double counts corner pixels, but my main questions are:

  • Is there a more concise way to merge a few ranges?
  • Why can I return a simple Range from the row method, but need to return a generator from the column method?
  • Is there anything else wrong with this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment