Maths formula of cleaner way for bounding box logic? - ios

Below I have written some bounding box logic which is a bit verbose. Is there a way to make it more readable, so the check looks cleaner and more concise?
func validatePosition(position:SCNVector3, forNode node:SCNNode) -> SCNVector3 {
var newPosition = position
var maxVector = SCNVector3Zero
var minVector = SCNVector3Zero
let success = self.actionDelegate?.getBoundingBox(&minVector, max: &maxVector)
guard success == true else {
return newPosition
}
if newPosition.x < minVector.x && newPosition.x < 0 {
newPosition.x = minVector.x
}
if newPosition.y < minVector.y && newPosition.y < 0 {
newPosition.y = minVector.y
}
if newPosition.z < minVector.z && newPosition.z < 0 {
newPosition.z = minVector.z
}
if newPosition.x > maxVector.x && newPosition.x > 0 {
newPosition.x = maxVector.x
}
if newPosition.y > maxVector.y && newPosition.y > 0 {
newPosition.y = maxVector.y
}
if newPosition.z > maxVector.z && newPosition.z > 0 {
newPosition.z = maxVector.z
}
return newPosition
}

Try this:
extension ClosedInterval {
func clamp(value : Bound) -> Bound {
return self.start > value ? self.start
: self.end < value ? self.end
: value
}
}
extension SCNVector3 {
func clamp(min min:SCNVector3, max: SCNVector3) -> SCNVector3 {
let x = (min.x...max.x).clamp(self.x)
let y = (min.y...max.y).clamp(self.y)
let z = (min.z...max.z).clamp(self.z)
return SCNVector3(x, y, z)
}
}
func validatePosition(position:SCNVector3, forNode node:SCNNode) -> SCNVector3 {
var newPosition = position
var maxVector = SCNVector3Zero
var minVector = SCNVector3Zero
let success = self.actionDelegate?.getBoundingBox(&minVector, max: &maxVector)
guard success == true else {
return newPosition
}
newPosition = position.clamp(min: minVector, max: maxVector)
return newPosition
}

Related

Get Index on UIslider

I have radar chart with data.I want to give index for slider Based on data count.If i am moving slider the graph value should move.If suppose i am at bellow of middle value slider thumb should go to near index.
for _ in 0..<3 {
let mind = arc4random_uniform(mult) + min
let bre = arc4random_uniform(mult) + min
let rel = arc4random_uniform(mult) + min
dataPoints.append(GraphDataPoints(mindfulness: Double(mind), breathing: Double(bre), relaxation: Double(rel)))
}
self.setChartData(mindfulness: dataPoints[0].mindfulness, breathing: dataPoints[0].breathing, relaxation: dataPoints[0].relaxation)
numberofPointsOnSlider = Float(100 / (dataPoints.count - 1))
numberofPointsOnSlider is percent to move.In lastSliderValue i am storing the slider value.pointIndex have index.
#objc func sliderTouchEvents(slider:UISlider,event: UIEvent){
slider.isContinuous = true
if let touchEvent = event.allTouches?.first{
switch touchEvent.phase{
case .began:
lastSliderValue = slider.value
break
case .moved:
if slider.value.truncatingRemainder(dividingBy: numberofPointsOnSlider) == 0{
if lastSliderValue < slider.value || lastSliderValue > slider.value{
pointIndex = Int(slider.value / numberofPointsOnSlider)
self.isIncrement = false
}
lastSliderValue = slider.value
}
if !animation {
if pointIndex >= 0 && pointIndex < dataPoints.count {
var percentToMove:Float = 0
if lastSliderValue > slider.value && pointIndex > 0 {
percentToMove = (100 * (numberofPointsOnSlider - (slider.value.truncatingRemainder(dividingBy: numberofPointsOnSlider) ))) / numberofPointsOnSlider
mindfulDiff = Float(dataPoints[pointIndex].mindfulness - dataPoints[pointIndex - 1].mindfulness)
relaxationDiff = Float(dataPoints[pointIndex].relaxation - dataPoints[pointIndex - 1].relaxation)
breathingDiff = Float(dataPoints[pointIndex].breathing - dataPoints[pointIndex - 1].breathing)
}else if lastSliderValue < slider.value && pointIndex < dataPoints.count - 1 {
percentToMove = (100 * (slider.value.truncatingRemainder(dividingBy: numberofPointsOnSlider) )) / numberofPointsOnSlider
mindfulDiff = Float(dataPoints[pointIndex].mindfulness - dataPoints[pointIndex + 1].mindfulness)
relaxationDiff = Float(dataPoints[pointIndex].relaxation - dataPoints[pointIndex + 1].relaxation)
breathingDiff = Float(dataPoints[pointIndex].breathing - dataPoints[pointIndex + 1].breathing)
}
mp = Float(dataPoints[pointIndex].mindfulness) - (mindfulDiff / 100 ) * percentToMove
rp = Float(dataPoints[pointIndex].relaxation) - (relaxationDiff / 100 ) * percentToMove
bp = Float(dataPoints[pointIndex].breathing) - (breathingDiff / 100) * percentToMove
UIView.animate(withDuration: 10, animations: {
self.setChartData(mindfulness: Double(self.mp) , breathing: Double(self.bp) , relaxation: Double(self.rp) )
})
}
else {
if (pointIndex < 0) {
pointIndex = 0
}
}
}
break
case .ended:
let progDiff = lastSliderValue - slider.value
print("End movie, \(pointIndex) , \(slider.value) ,progDiff: \(progDiff)")
if progDiff < 0{
if (progDiff * -1) >= numberofPointsOnSlider / 2 {
animation = true
UIView.animate(withDuration: 10, animations: {
slider.value += self.numberofPointsOnSlider + progDiff
})
pointIndex = Int(slider.value / numberofPointsOnSlider)+1
// if isIncrement {
// pointIndex += pointIndex
// }else {
// isIncrement = true
//
// }
mindfulDiff = Float(dataPoints[pointIndex].mindfulness - Double(mp))
relaxationDiff = Float(dataPoints[pointIndex].relaxation - Double(rp))
breathingDiff = Float(dataPoints[pointIndex].breathing - Double(bp))
doGraphAnimate()
}else {
animation = true
UIView.animate(withDuration: 10, animations: {
slider.value = self.lastSliderValue
})
mindfulDiff = Float(dataPoints[pointIndex].mindfulness - Double(mp))
relaxationDiff = Float(dataPoints[pointIndex].relaxation - Double(rp))
breathingDiff = Float(dataPoints[pointIndex].breathing - Double(bp))
doGraphAnimate()
}
}else if progDiff > 0{
if progDiff >= numberofPointsOnSlider / 2 {
animation = true
// if isIncrement {
// print("pointIndex : \(pointIndex)")
// pointIndex += pointIndex
// print("pointIndex Incremented : \(pointIndex)")
// }else{
// isIncrement = true
// }
pointIndex = Int(slider.value / numberofPointsOnSlider)
UIView.animate(withDuration: 10, animations: {
slider.value -= self.numberofPointsOnSlider - (self.lastSliderValue - slider.value)
})
mindfulDiff = Float(dataPoints[pointIndex].mindfulness - Double(mp))
relaxationDiff = Float(dataPoints[pointIndex].relaxation - Double(rp))
breathingDiff = Float(dataPoints[pointIndex].breathing - Double(bp))
doGraphAnimate()
}
else {
animation = true
UIView.animate(withDuration: 10, animations: {
slider.value = self.lastSliderValue
})
mindfulDiff = Float(dataPoints[pointIndex].mindfulness - Double(mp))
relaxationDiff = Float(dataPoints[pointIndex].relaxation - Double(rp))
breathingDiff = Float(dataPoints[pointIndex].breathing - Double(bp))
doGraphAnimate()
}
}
default :
break
}
}
}
Help me, Thankyou .
DO you mean you want slider to move only at certain discrete positions when you move it ? But your spec is not totally clear.
If so, this is how I, so that if slider moves from 0 to 14 it will show 14, from 15 to 85, it will show 85. I let you adapt to get the exact working you want.
I use this IBAction for the slider
#IBAction fileprivate func changeRowSlider(_ sender: NSSlider) {
let movedPosition = Int(sender.doubleValue)
if movedPosition == 0 {
sender.integerValue = 0
return
}
if movedPosition <= 14 {
sender.integerValue = 14
return
}
if movedPosition <= 85 {
sender.integerValue = 85
return
}
sender.integerValue = 100 // last case: move to maxValue
}

How do I run a function inside an action in Sprite Kit?

I have the following code for a game I am making. In one of the parts, I have
let spawn = SKAction.run {
self.createCars()
}
but for some reason I keep getting an error saying "Value of type '(NSObject -> () -> GameScene' has no member 'createCars' even though createCars is a function defined lower in my code. Why is this happening and what can I do to fix this?
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var lanecounter:CGFloat = 0
var score:Int = 0
var player:SKSpriteNode?
let scoreLabel = SKLabelNode()
let highScoreLabel = SKLabelNode()
var direction:Int?
let noCategory:UInt32 = 0
let carCategory:UInt32 = 0b1
let playerCategory:UInt32 = 0b1 << 1
let pointCategory:UInt32 = 0b1 << 2
var died:Bool?
var restartBTN = SKSpriteNode()
let moveLeft:SKAction = SKAction.moveBy(x: -100, y: 0, duration: 0.065)
let moveRight:SKAction = SKAction.moveBy(x: 100, y: 0, duration: 0.065)
let moveDown:SKAction = SKAction.moveTo(y: -800, duration: 1.1)
let spawn = SKAction.run {
self.createCars()
}
let delay = SKAction.wait(forDuration: 4)
func createCars(){
let middlePoints = SKSpriteNode()
var openLane:CGFloat = 0
middlePoints.size = CGSize(width: 100, height: 10)
middlePoints.physicsBody = SKPhysicsBody(rectangleOf: middlePoints.size)
//middlePoints.color = SKColor.purple
middlePoints.physicsBody?.categoryBitMask = pointCategory
middlePoints.physicsBody?.collisionBitMask = playerCategory
middlePoints.physicsBody?.contactTestBitMask = playerCategory
var randInt:Int = 0
randInt = Int(arc4random_uniform(3))
if lanecounter == -2 {
if randInt == 0 || randInt == 1 {
openLane = -1
}
if randInt == 2 {
openLane = -2
}
}
if lanecounter == 2 {
if randInt == 0 || randInt == 1 {
openLane = 1
}
if randInt == 2 {
openLane = 2
}
}
if lanecounter == -1 || lanecounter == 0 || lanecounter == 1 {
if randInt == 0 {
openLane = lanecounter - 1
}
if randInt == 1 {
openLane = lanecounter
}
if randInt == 2 {
openLane = lanecounter + 1
}
}
//print("lanecounter is", lanecounter)
//print("open lane is", openLane)
if openLane != -2 {
spawnCar(xCord: -2)
}
if openLane != -1 {
spawnCar(xCord: -1)
}
if openLane != 0 {
spawnCar(xCord: 0)
}
if openLane != 1 {
spawnCar(xCord: 1)
}
if openLane != 2 {
spawnCar(xCord: 2)
}
middlePoints.position = CGPoint(x: (openLane*100), y: self.frame.height)
self.addChild(middlePoints)
middlePoints.run(moveDown)
}
}
Minimal, Complete, and Verifiable
First of all, we need a Minimal, Complete, and Verifiable example like this:
class GameScene: SKScene {
let spawn = SKAction.run {
self.createCars()
}
func createCars() { }
}
error: value of type '(NSObject) -> () -> GameScene' has no member 'createCars'
Problem
The problem happens because you can't populate a property on the same line it is defined using self (infact self doesn't exist yet).
Solution
However you can use a lazy property wich will be evaluated only when called (and at that time self will be available).
class GameScene: SKScene {
lazy var spawn: SKAction = {
SKAction.run { self.createCars() }
}()
func createCars() { }
}

Make PanGesture becoming to slider

I made a PanGuesture as a slider, Here blow is the responds func:
func respondsToPenGesture(sender: UIPanGestureRecognizer) {
if (sender.state == UIGestureRecognizerState.Began) {
if noFilterEffectButton.selected == false {
startPanLocation = sender.locationInView(self.newEffectView)
persentageNumber.text = String(startNumber)
persentageNumber.hidden = false
}
} else if (sender.state == UIGestureRecognizerState.Changed) {
let stopLocation = sender.locationInView(self.newEffectView)
let abscissaChange = stopLocation.x - startPanLocation!.x
if newEffectView.hidden == false {
if abs(abscissaChange) > 0 {
if noFilterEffectButton.selected == false{
if let effectCurrent = self.currentEffect {
effectCurrent.adjustParams(CGFloat(abscissaChange/6))
}
}
}
if noFilterEffectButton.selected == false {
var printChangingNumber = startNumber + Int(abscissaChange)
if printChangingNumber > 100 {
printChangingNumber = 100
} else if printChangingNumber < 0 {
printChangingNumber = 0
}
persentageNumber.text = String(printChangingNumber)
}
}
} else if (sender.state == UIGestureRecognizerState.Ended) {
let stopLocation = sender.locationInView(self.newEffectView)
let abscissaChange = stopLocation.x - startPanLocation!.x
startNumber = startNumber + Int(abscissaChange)
if startNumber > 100 {
startNumber = 100
} else if startNumber < 0 {
startNumber = 0
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.5*Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
self.persentageNumber.text = String(self.startNumber)
self.persentageNumber.hidden = true
}
}
}
percentageNumber is a label show the changes on the screen. And the adjustParams() function:
func adjustParams(value: CGFloat) {
newValue = value + newValue
self.adjust = newValue
print("AdjustNumber")
print(adjust)
}
And I override this function for exactly one effect:
override func adjustParams(value: CGFloat) {
super.adjustParams(value)
lastAutValueChange = lastAutValueChange + value/200
lastIntensityChange = lastIntensityChange + value/200
var currentAutValue = lastAutValueChange
var currentIntValue = lastIntensityChange
currentIntValue = currentIntValue < -1.0 ? -1.0 : currentIntValue
//value = value > 1.0 ? 1.0 : value
//autValue = value < -1.0 ? -1.0 : autValue
if (currentIntValue >= 0) {
currentIntValue = 1.0
} else {
currentIntValue += 1
}
if (currentAutValue <= 0) {
currentAutValue = 0
} else {
if currentAutValue > 1 {
currentAutValue = 1
}
}
print(currentIntValue)
print(currentAutValue)
self.autoTuneModule.setIntensity(Float(currentIntValue))
self.audioUnit.finalMix = Double(currentAutValue)
}
The test answer is not matchable for the exactly changes and the percentNumber shows on the screen. How could I change my functions to make them matchable for each others?
(By the way, I want to do something like the slider in Prisma, if there is something like it, please let me know about it).

Logarithmic y-axis in swift?

I'm working on a financial application that has some quite large numbers.
One of my charts for example is growing from $25,000 to about $20,000,000,000 (exponentially)
Because of this, using any regular chart without log-scale causes the majority of my data to simply "flatline", while it's in fact growing quickly.
Here's an example
(Ignore that it's not set up yet)
The above linechart was made using SwiftChart
Now I have to be honest, I am not even at this point entirely sure if I'm even allowed to ask a question like this on SO. But I just have no idea where to begin.
After a couple of hours of searching I have not been able to find any libraries, tutorials, sample apps or anything IOS related that features a graph with a logarithmic axis (neither X or Y).
After I couldn't find anything, I realised I'd have to implement this feature on my own. I found the most suitable library for my application, and I've been trying to figure out how I can implement a way to allow the y-scale to be scaled with a log-scale (with the power of 10).
I've done a fair bit of research on how log-scales work, and used them plenty in my university. But I have never actually seen an example of one programmed.
I found one written in JavaScript (I think). But unfortunately I have no experience with web-development and wasn't able to properly translate it :(
I really don't feel comfortable putting someone elses code on SO, but I really have no clue where to begin.
If this isn't allowed, please post a comment, and I'll get rid of it immediately
All credits of this goes to this guy: https://github.com/gpbl/SwiftChart
Here's the code that I believe is where I need to implement a log-scale.
private func drawChart() {
drawingHeight = bounds.height - bottomInset - topInset
drawingWidth = bounds.width
let minMax = getMinMax()
min = minMax.min
max = minMax.max
highlightShapeLayer = nil
// Remove things before drawing, e.g. when changing orientation
for view in self.subviews {
view.removeFromSuperview()
}
for layer in layerStore {
layer.removeFromSuperlayer()
}
layerStore.removeAll()
// Draw content
for (index, series) in enumerate(self.series) {
// Separate each line in multiple segments over and below the x axis
var segments = Chart.segmentLine(series.data as ChartLineSegment)
for (i, segment) in enumerate(segments) {
let scaledXValues = scaleValuesOnXAxis( segment.map( { return $0.x } ) )
let scaledYValues = scaleValuesOnYAxis( segment.map( { return $0.y } ) )
if series.line {
drawLine(xValues: scaledXValues, yValues: scaledYValues, seriesIndex: index)
}
if series.area {
drawArea(xValues: scaledXValues, yValues: scaledYValues, seriesIndex: index)
}
}
}
drawAxes()
if xLabels != nil || series.count > 0 {
drawLabelsAndGridOnXAxis()
}
if yLabels != nil || series.count > 0 {
drawLabelsAndGridOnYAxis()
}
}
// MARK: - Scaling
private func getMinMax() -> (min: ChartPoint, max: ChartPoint) {
// Start with user-provided values
var min = (x: minX, y: minY)
var max = (x: maxX, y: maxY)
// Check in datasets
for series in self.series {
let xValues = series.data.map( { (point: ChartPoint) -> Float in
return point.x } )
let yValues = series.data.map( { (point: ChartPoint) -> Float in
return point.y } )
let newMinX = minElement(xValues)
let newMinY = minElement(yValues)
let newMaxX = maxElement(xValues)
let newMaxY = maxElement(yValues)
if min.x == nil || newMinX < min.x! { min.x = newMinX }
if min.y == nil || newMinY < min.y! { min.y = newMinY }
if max.x == nil || newMaxX > max.x! { max.x = newMaxX }
if max.y == nil || newMaxY > max.y! { max.y = newMaxY }
}
// Check in labels
if xLabels != nil {
let newMinX = minElement(xLabels!)
let newMaxX = maxElement(xLabels!)
if min.x == nil || newMinX < min.x { min.x = newMinX }
if max.x == nil || newMaxX > max.x { max.x = newMaxX }
}
if yLabels != nil {
let newMinY = minElement(yLabels!)
let newMaxY = maxElement(yLabels!)
if min.y == nil || newMinY < min.y { min.y = newMinY }
if max.y == nil || newMaxY > max.y { max.y = newMaxY }
}
if min.x == nil { min.x = 0 }
if min.y == nil { min.y = 0 }
if max.x == nil { max.x = 0 }
if max.y == nil { max.y = 0 }
return (min: (x: min.x!, y: min.y!), max: (x: max.x!, max.y!))
}
private func scaleValuesOnXAxis(values: Array<Float>) -> Array<Float> {
let width = Float(drawingWidth)
var factor: Float
if max.x - min.x == 0 { factor = 0 }
else { factor = width / (max.x - min.x) }
let scaled = values.map { factor * ($0 - self.min.x) }
return scaled
}
private func scaleValuesOnYAxis(values: Array<Float>) -> Array<Float> {
let height = Float(drawingHeight)
var factor: Float
if max.y - min.y == 0 { factor = 0 }
else { factor = height / (max.y - min.y) }
let scaled = values.map { Float(self.topInset) + height - factor * ($0 - self.min.y) }
return scaled
}
private func scaleValueOnYAxis(value: Float) -> Float {
let height = Float(drawingHeight)
var factor: Float
if max.y - min.y == 0 { factor = 0 }
else { factor = height / (max.y - min.y) }
let scaled = Float(self.topInset) + height - factor * (value - min.y)
return scaled
}
private func getZeroValueOnYAxis() -> Float {
if min.y > 0 {
return scaleValueOnYAxis(min.y)
}
else {
return scaleValueOnYAxis(0)
}
}
// MARK: - Drawings
private func isVerticalSegmentAboveXAxis(yValues: Array<Float>) -> Bool {
// YValues are "reverted" from top to bottom, so min is actually the maxz
let min = maxElement(yValues)
let zero = getZeroValueOnYAxis()
return min <= zero
}
private func drawLine(#xValues: Array<Float>, yValues: Array<Float>, seriesIndex: Int) -> CAShapeLayer {
let isAboveXAxis = isVerticalSegmentAboveXAxis(yValues)
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, CGFloat(xValues.first!), CGFloat(yValues.first!))
for i in 1..<yValues.count {
let y = yValues[i]
CGPathAddLineToPoint(path, nil, CGFloat(xValues[i]), CGFloat(y))
}
var lineLayer = CAShapeLayer()
lineLayer.frame = self.bounds
lineLayer.path = path
if isAboveXAxis {
lineLayer.strokeColor = series[seriesIndex].colors.above.CGColor
}
else {
lineLayer.strokeColor = series[seriesIndex].colors.below.CGColor
}
lineLayer.fillColor = nil
lineLayer.lineWidth = lineWidth
lineLayer.lineJoin = kCALineJoinBevel
self.layer.addSublayer(lineLayer)
layerStore.append(lineLayer)
return lineLayer
}
private func drawArea(#xValues: Array<Float>, yValues: Array<Float>, seriesIndex: Int) {
let isAboveXAxis = isVerticalSegmentAboveXAxis(yValues)
let area = CGPathCreateMutable()
let zero = CGFloat(getZeroValueOnYAxis())
CGPathMoveToPoint(area, nil, CGFloat(xValues[0]), zero)
for i in 0..<xValues.count {
CGPathAddLineToPoint(area, nil, CGFloat(xValues[i]), CGFloat(yValues[i]))
}
CGPathAddLineToPoint(area, nil, CGFloat(xValues.last!), zero)
var areaLayer = CAShapeLayer()
areaLayer.frame = self.bounds
areaLayer.path = area
areaLayer.strokeColor = nil
if isAboveXAxis {
areaLayer.fillColor = series[seriesIndex].colors.above.colorWithAlphaComponent(areaAlphaComponent).CGColor
}
else {
areaLayer.fillColor = series[seriesIndex].colors.below.colorWithAlphaComponent(areaAlphaComponent).CGColor
}
areaLayer.lineWidth = 0
self.layer.addSublayer(areaLayer)
layerStore.append(areaLayer)
}
private func drawAxes() {
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, axesColor.CGColor)
CGContextSetLineWidth(context, 0.5)
// horizontal axis at the bottom
CGContextMoveToPoint(context, 0, drawingHeight + topInset)
CGContextAddLineToPoint(context, drawingWidth, drawingHeight + topInset)
CGContextStrokePath(context)
// horizontal axis at the top
CGContextMoveToPoint(context, 0, 0)
CGContextAddLineToPoint(context, drawingWidth, 0)
CGContextStrokePath(context)
// horizontal axis when y = 0
if min.y < 0 && max.y > 0 {
let y = CGFloat(getZeroValueOnYAxis())
CGContextMoveToPoint(context, 0, y)
CGContextAddLineToPoint(context, drawingWidth, y)
CGContextStrokePath(context)
}
// vertical axis on the left
CGContextMoveToPoint(context, 0, 0)
CGContextAddLineToPoint(context, 0, drawingHeight + topInset)
CGContextStrokePath(context)
// vertical axis on the right
CGContextMoveToPoint(context, drawingWidth, 0)
CGContextAddLineToPoint(context, drawingWidth, drawingHeight + topInset)
CGContextStrokePath(context)
}
Any hint or help as to where I need to implement this would be massively appreciated.
And I apologize for the awfulness that is this question begging for help or a direction.

How to get all Points Along a Line in Swift?

I am writing a program in Swift which requires a method that uses specification of two CGPoints and returns all points in a straight line in-between them. At the moment I am trialling the following method, but it is very glitchy and refuses to get all points for some lines. I was just wondering if there is a more efficient way of doing this?
func addPointsInLine(#start: CGPoint, end: CGPoint){
var speed = 0
var startNo = trackPoints.count
var endNo = startNo
var xDiff = start.x - end.x
var yDiff = start.y - end.y
var xChange = 0.0
var yChange = 0.0
var newPointX = start.x
var newPointY = start.y
var ended = false
xChange = Double(xDiff) / Double(yDiff)
yChange = Double(yDiff) / Double(xDiff)
/*if(xDiff > 0){
xChange = sqrt(xChange * xChange)
}
if(yDiff > 0){
yChange = sqrt(yChange * yChange)
}*/
println("xc \(xChange)")
println("yc \(yChange)")
var y = Double(start.y)
var x = Double(start.x)
while !ended {
println(trackPoints.count)
speed++
endNo++
if(CGPointMake(newPointX, newPointY) == end){
ended = true
}
if(yChange > xChange){
y++
x += xChange
trackPoints.append(TrackPoint(x: Int(x), y: Int(y)))
if(CGFloat(Int(y)) == end.y){
ended = true
println("end")
//break
}
/* if(yChange > 0){
if(CGFloat(Int(y)) > end.y){
ended = true
println("end>y")
// break
}
}
if(yChange < 0){
if(CGFloat(Int(y)) < end.y){
ended = true
println("end<y")
//break
}
}*/
if(xChange > 0){
if(CGFloat(Int(x)) >= end.x){
ended = true
println("end>x")
//break
}
}
if(xChange < 0){
if(CGFloat(Int(x)) <= end.x){
ended = true
println("end<x")
//break
}
}
} else {
x++
y += yChange
trackPoints.append(TrackPoint(x: Int(x), y: Int(y)))
if(CGFloat(Int(x)) == end.x){
ended = true
println("end")
//break
}
/* if(xChange > 0){
if(CGFloat(Int(x)) >= end.x){
ended = true
println("end>x")
//break
}
}
if(xChange < 0){
if(CGFloat(Int(x)) <= end.x){
ended = true
println("end<x")
//break
}
}*/
if(yChange > 0){
if(CGFloat(Int(y)) > end.y){
ended = true
println("end>y")
// break
}
}
if(yChange < 0){
if(CGFloat(Int(y)) < end.y){
ended = true
println("end<y")
//break
}
}
}
}
println("finished")
var i = startNo
println(startNo)
println(endNo)
while i < endNo {
trackPoints[i].speed = speed
i++
}
println("finish2")
}
I think firstly finding all points isn't possible because there are infinite points in a straight line. Take example of line joining (0,0) to (1, 0). All the following points and many more are on the said line (0.00001,0) (0.0000000000001,0) (0.01,0)
So you need to limit the amount of points you need to find like all the points with Integer co-ordinates. All the points 1 unit apart, starting from starting point etc.
Next you can use one of equations of line to get points: Equations of Line
Try this,
func findAllPointsBetweenTwoPoints(startPoint : CGPoint, endPoint : CGPoint) {
var allPoints :[CGPoint] = [CGPoint]()
let deltaX = fabs(endPoint.x - startPoint.x)
let deltaY = fabs(endPoint.y - startPoint.y)
var x = startPoint.x
var y = startPoint.y
var err = deltaX-deltaY
var sx = -0.5
var sy = -0.5
if(startPoint.x<endPoint.x){
sx = 0.5
}
if(startPoint.y<endPoint.y){
sy = 0.5;
}
repeat {
let pointObj = CGPoint(x: x, y: y)
allPoints.append(pointObj)
let e = 2*err
if(e > -deltaY)
{
err -= deltaY
x += CGFloat(sx)
}
if(e < deltaX)
{
err += deltaX
y += CGFloat(sy)
}
} while (round(x) != round(endPoint.x) && round(y) != round(endPoint.y));
allPoints.append(endPoint)
}

Resources