var xAxisImpulse = 0
var yAxisImpulse = 0
if angleToShoot < 90 && angleToShoot > 0 {
xAxisImpulse = cos(angleToShoot)*45 //cos is in radians
}
There is an error at the cos(). It says that because the angleToShoot var is an int, it can not be executed. How can I change this so that this can work?
You need to use CGFloat or Double as that this the data type used by the cos function.
var angleToShoot : CGFloat = 0.0
var xAxisImpulse : CGFloat = 0.0
var yAxisImpulse : CGFloat = 0.0
if angleToShoot < 90 && angleToShoot > 0 {
xAxisImpulse = cos(angleToShoot)*45 //cos is in radians
}
Related
When some values are small in QML pie chart, slice labels are messed up:
How can I arrange slice labels like this?
Note that this is available in telerik and /or dev components for c#.
I used of #luffy 's comment and with some modification, I reached to following code and result:
import QtQuick 2.4
Rectangle {
id: root
// public
property string fontFamily: "sans-serif"
property int fontPointSize: 9
property double donutHoleSize: 0.4 //0~1
property string title: 'title'
property variant points: []//[['Zero', 60, 'red'], ['One', 40, 'blue']] // y values don't need to add to 100
width: 500
height: 700
// private
onPointsChanged: myCanvas.requestPaint()
Canvas {
id: myCanvas
anchors.fill: parent
property double factor: Math.min(width, height)
Text { // title
text: title
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: 0.03 * myCanvas.factor
}
onPaint: {
var context = getContext("2d")
var total = 0 // automatically calculated from points.y
var start = -Math.PI / 2 // Start from vertical. 0 is 3 o'clock and positive is clockwise
var radius = 0.25 * myCanvas.factor
var pixelSize = 0.03 * myCanvas.factor // text
context.font = root.fontPointSize + 'pt ' + root.fontFamily
var i = 0;
for(i = 0; i < points.length; i++) total += points[i][1] // total
context.clearRect(0, 0, width, height) // new points data
//--------------------------------------------------------
var end = 0;
var center = 0;
var angle = 0;
var midSlice = 0;
var point = 0;
var topRightCnt = 0
var bottomRightCnt = 0
var topLeftCnt = 0
var bottomLeftCnt = 0
var itemsPos = []
center = Qt.vector2d(width / 2, height / 2) // center
for(i = 0; i < points.length; i++) {
end = start + 2 * Math.PI * points[i][1] / total // radians
angle = (start + end) / 2 // of line
midSlice = Qt.vector2d(Math.cos((end + start) / 2), Math.sin((end + start) / 2)).times(radius) // point on edge/middle of slice
point = midSlice.times(1 + 1.4 * (1 - Math.abs(Math.cos(angle)))).plus(center) // elbow of line
if(point.x<center.x && point.y<=center.y) {
topLeftCnt++;
itemsPos[i] = "tl"
}
else if(point.x<center.x && point.y>center.y) {
bottomLeftCnt++;
itemsPos[i] = "bl"
}
else if(point.x>=center.x && point.y<=center.y) {
topRightCnt++;
itemsPos[i] = "tr"
}
else {
bottomRightCnt++;
itemsPos[i] = "br"
}
start = end // radians
}
//--------------------------------------------------------
end = 0;
angle = 0;
midSlice = 0;
point = 0;
var itemPosCounterTR = 0;
var itemPosCounterTL = 0;
var itemPosCounterBR = 0;
var itemPosCounterBL = 0;
for(i = 0; i < points.length; i++) {
end = start + 2 * Math.PI * points[i][1] / total // radians
// pie
context.fillStyle = points[i][2]
context.beginPath()
midSlice = Qt.vector2d(Math.cos((end + start) / 2), Math.sin((end + start) / 2)).times(radius) // point on edge/middle of slice
context.arc(center.x, center.y, radius, start, end) // x, y, radius, startingAngle (radians), endingAngle (radians)
context.lineTo(center.x, center.y) // center
context.fill()
// text
context.fillStyle = points[i][2]
angle = (start + end) / 2 // of line
point = midSlice.times(1 + 1.4 * (1 - Math.abs(Math.cos(angle)))).plus(center) // elbow of line
//---------------------------------------------
var textX = 0;
var textY = 0;
var dis = 0;
var percent = points[i][1] / total * 100
var text = points[i][0] + ': ' + (percent < 1? '< 1': Math.round(percent)) + '%' // display '< 1%' if < 1
var textWidth = context.measureText(text).width
var textHeight = 15
var diameter = radius * 2
var topCircle = center.y - radius
var leftCircle = center.x - radius
if(itemsPos[i] === "tr") {
textX = leftCircle + 1.15 * diameter
dis = Math.floor((1.15*radius) / topRightCnt) //Math.floor((height/2) / topRightCnt)
dis = (dis < 20 ? 20 : dis)
textY = topCircle -(0.15*diameter) + (itemPosCounterTR*dis) + (textHeight/2)
itemPosCounterTR++
}
else if(itemsPos[i] === "br") {
textX = leftCircle + 1.15 * diameter
dis = Math.floor((1.15*radius) / bottomRightCnt)
dis = (dis < 20 ? 20 : dis)
textY = topCircle+(1.15*diameter) - ((bottomRightCnt-itemPosCounterBR-1)*dis) - (textHeight/2)
itemPosCounterBR++
}
else if(itemsPos[i] === "tl") {
textX = leftCircle - (0.15 * diameter) - textWidth
dis = Math.floor((1.15*radius) / topLeftCnt)
dis = (dis < 20 ? 20 : dis)
textY = topCircle-(0.15*diameter) + ((topLeftCnt-itemPosCounterTL-1)*dis) + (textHeight/2)
itemPosCounterTL++;
}
else {
textX = leftCircle - (0.15 * diameter) - textWidth //-0.2 * width - textWidth
dis = Math.floor((1.15*radius) / bottomLeftCnt)
dis = (dis < 20 ? 20 : dis)
textY = topCircle+(1.15*diameter) - (itemPosCounterBL*dis) - (textHeight/2)
itemPosCounterBL++
}
//---------------------------------------------
context.fillText(text, textX, textY)
// line
context.lineWidth = 1
context.strokeStyle = points[i][2]
context.beginPath()
context.moveTo(center.x + midSlice.x, center.y + midSlice.y) // center
var endLineX = (point.x < center.x ? (textWidth + 0.5 * pixelSize) : (-0.5 * pixelSize)) + textX;
context.lineTo(endLineX, textY+3)
context.lineTo(endLineX + (point.x < center.x? -1: 1) * ((0.5 * pixelSize)+textWidth), textY+3) // horizontal
context.stroke()
start = end // radians
}
if(root.donutHoleSize > 0) {
root.donutHoleSize = Math.min(0.99, root.donutHoleSize);
var holeRadius = root.donutHoleSize * radius;
context.fillStyle = root.color
context.beginPath()
context.arc(center.x, center.y, holeRadius, 0, 2*Math.PI) // x, y, radius, startingAngle (radians), endingAngle (radians)
//context.lineTo(center.x, center.y) // center
context.fill()
}
}
}
}
And it's result:
Thanks #luffy.
I tried many different code to have a parallel loop faster than a sequential one.
My latest attempt :
startTime = CFAbsoluteTimeGetCurrent()
var totaliter = 0
for x in stride(from: -2.0, to: 2.0, by: delta) {
for y in stride(from: -2.0, to: 2.0, by: delta) {
let cx : Double = x
let cy : Double = y
var zx : Double = 0.0
var zy : Double = 0.0
var iteration : Int = 0
while (zx * zx + zy * zy < 4.0 && iteration < maxIter) {
let tmp = zx * zx - zy * zy + cx
zy = 2.0 * zx * zy + cy
zx = tmp
iteration += 1
totaliter += 1
}
}
}
print(totaliter)
print("\tTime : \(CFAbsoluteTimeGetCurrent() - startTime)")
this sequential code take 4s.
The following using swift-atomic code take 29s :
startTime = CFAbsoluteTimeGetCurrent()
let atomicInc = ManagedAtomic<UInt32>(0)
DispatchQueue.concurrentPerform(iterations: Int(4/delta)) { (xIter) in
for y in stride(from: -2.0, to: 2.0, by: delta) {
let x = -2.0 + (Double(xIter) * delta)
let cx : Double = x
let cy : Double = y
var zx : Double = 0.0
var zy : Double = 0.0
var iteration : Int = 0
while (zx * zx + zy * zy < 4.0 && iteration < maxIter) {
let tmp = zx * zx - zy * zy + cx
zy = 2.0 * zx * zy + cy
zx = tmp
iteration += 1
atomicInc.wrappingIncrement(ordering: AtomicUpdateOrdering.relaxed)
}
}
}
print("\n\tMeaningless number (race condition) : ",atomicInc.load(ordering: .relaxed))
print("\tTime : \(CFAbsoluteTimeGetCurrent() - startTime)")
Replacing the atomicInc.wrappingIncrement by a NSLock lock/unlock take forever, I never had the patience to let it finish. my guess is around 10mn and the cpu is 80% busy on "system" load (as opposed to user load)
I simply can't find a way to have a parallel loop that is faster than the sequential version. All my attempts either failed or had a race condition.
For reference, my previous attempt with a different computation was like this (and, yes, the sequential version was faster than the swift-atomic or the NSLock version, I assumed that drand48 was the culprit, but it seems I was wrong)
DispatchQueue.concurrentPerform(iterations: 100) { (_) in
for _ in 1...iteration / 100 {
let rand_x : Float = Float(drand48())
let rand_y : Float = Float(drand48())
let origin_dist : Float = rand_x * rand_x + rand_y * rand_y
if (origin_dist <= 1) {
atomicinside.wrappingIncrement(ordering: AtomicUpdateOrdering.relaxed)
}
}
}
How would you do a parallel loop that require updating a non-thread-local variable ? Anything will do as long as it's fast. I'm using concurrentPerform because I don't know any better, but I'm open to suggestions.
fun fact : the parallel version is slower while using 4~8x more CPU.
You should take the locking increment out of the inner loop and do a locking
totaliter += iteration
I have this swift code:
x = Float(arc4random_uniform(30))
y = Float(arc4random_uniform(30))
z = Float(arc4random_uniform(30))
coords.x = 0.00 - x
coords.y = 0.00 - y
coords.z = 0.00 - z
print("\(coords.x) \(coords.y) \(coords.z) xyz")
For some reason it always outputs the numbers as positive, I have also tried:
x = -Float(arc4random_uniform(30))
y = -Float(arc4random_uniform(30))
z = -Float(arc4random_uniform(30))
coords.x = x
coords.y = y
coords.z = z
and...
x = -1 * Float(arc4random_uniform(30))
y = -1 * Float(arc4random_uniform(30))
z = -1 * Float(arc4random_uniform(30))
coords.x = x
coords.y = y
coords.z = z
...and even one more where I had to cast the random number as an Int to multiply it by -1 within the Float cast.
x = Float(-1*Int(arc4random_uniform(30)))
...
The console output is always something like:
24.0 27.0 29.0 xyz
...no negative numbers.
What am I doing wrong?
EDIT
coords is of type MyDict which I created:
class MyDict : NSDictionary {
var x: Float = 0.0
var y: Float = 0.0
var z: Float = 0.0
}
This is how I am printing the values:
print("\(coords.x) \(coords.y) \(coords.z) xyz")
EDIT
MyDict is now:
struct Coords {
var x: Float = 0.0
var y: Float = 0.0
var z: Float = 0.0
}
EDIT
Code for context - this is happening in a loop:
for nodeInnermost in nodeInner.childNodes {
if (nodeInnermost.name?.localizedStandardContains("ship"))! {
print(nodeInnermost.childNodes.first ?? "FIRST DOESNT EXIST")
var seqArray = [fadeOut, fadeIn]
var displacements = [] as Array<Coords>
var count = 0
var coords = Coords()
while count < 5 {
if childCount < 5 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 4 && childCount < 10 {
coords.x = -Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 9 && childCount < 15 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 14 && childCount < 20 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 19 && childCount < 25 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 24 && childCount < 30 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 29 && childCount < 35 {
coords.x = Float(arc4random_uniform(30))
coords.y = Float(arc4random_uniform(30))
coords.z = Float(arc4random_uniform(30))
}
else if childCount > 34 && childCount < 40 {
coords.x = -Float(arc4random_uniform(30))
coords.y = -Float(arc4random_uniform(30))
coords.z = -Float(arc4random_uniform(30))
}
//print("\(x) \(y) \(z) xyz")
displacements.append(coords)
print("\(coords.x) \(coords.y) \(coords.z) xyz")
let moveBy = SCNAction.move(by: SCNVector3Make(coords.x, coords.y, coords.z), duration: 0.5)
seqArray.append(fadeOut)
seqArray.append(moveBy)
seqArray.append(fadeIn)
count+=1
}
while count < 10 {
let moveBy = SCNAction.move(by: SCNVector3Make(displacements[9 - count].x, displacements[9 - count].y, displacements[9 - count].z), duration: 0.5)
seqArray.append(fadeOut)
seqArray.append(moveBy)
seqArray.append(fadeIn)
count+=1
}
let sequence = SCNAction.sequence(seqArray)
let animation = SCNAction.repeatForever(sequence)
nodeInnermost.childNodes.first?.runAction(animation)
}
}
SAMPLE OUTPUT:
https://pastebin.com/Ak1pb6wP
It makes no sense for your MyDict to extend NSDictionary nor should it be a class. Once you make it a proper struct, your code works fine.
struct MyDict {
var x: Float = 0.0
var y: Float = 0.0
var z: Float = 0.0
}
var coords = MyDict()
coords.x = -Float(arc4random_uniform(30))
coords.y = -Float(arc4random_uniform(30))
coords.z = -Float(arc4random_uniform(30))
print("\(coords.x) \(coords.y) \(coords.z) xyz")
Output:
-24.0 -28.0 -4.0 xyz
I tried to translate the MATLAB language on Cordic wikipedia webpage
However when I type those:
print(cordic(beta: Double.pi/9, n: 20))
print(cordic(beta: Double.pi/8, n: 20))
I get
[-0.17163433840184755, 0.98516072489744066]
[-0.17163433840184755, 0.98516072489744066]
It's always giving me a constant answer. Why? I'm sure that the "angle" and "Kvalues" arrays are properly calculated.
Here's the code:
import Foundation
var angles: [Double] = []
for i: Double in stride(from: 0, to: 27, by: 1) {
angles.append(atan(pow(2, -i)))
}
var Kvalues: [Double] = []
for i: Double in stride(from: 0, to: 23, by: 1) {
Kvalues.append(1/sqrt(abs(Double(1) + pow(2,-2 * i))))
if i > 0 {
Kvalues[Kvalues.count - 1] *= Kvalues[Kvalues.count - 2]
}
}
func min(_ a: Int, _ b: Int) -> Int {
return a > b ? b : a
}
func cordic(beta: Double, n: Int) -> [Double] {
var beta1 = beta
let Kn = Kvalues[min(n, Kvalues.count - 1)]
var v: [Double] = [1,0]
var poweroftwo: Double = 1
var angle = angles[0]
for j in 0 ..< n {
let sigma: Double = beta < 0 ? -1 : 1
let factor: Double = sigma * poweroftwo
v = [v[0] - v[1] * factor, v[1] + v[0] * factor]
beta1 -= sigma * angle
poweroftwo /= 2
angle = j + 2 > angles.count ? angle / 2 : angles[j + 2]
}
return [v[0] * Kn, v[1] * Kn]
}
print(cordic(beta: Double.pi/9, n: 20))
print(cordic(beta: Double.pi/8, n: 20))
You get the same result for different input because in
let sigma: Double = beta < 0 ? -1 : 1
beta should be beta1, which is the local variable that is
updated in the loop.
But even after fixing that the results are not correct, and that is
caused by two "off-by-one" index errors. The arrays in the algorithm
description are 1-based and Swift arrays are 0-based. So
let Kn = Kvalues[min(n, Kvalues.count - 1)]
// should be
let Kn = Kvalues[min(n-1, Kvalues.count - 1)]
and
angle = j + 2 > angles.count ? angle / 2 : angles[j + 2]
// should be
angle = j + 1 >= angles.count ? angle / 2 : angles[j + 1]
The angles and Kvalues arrays should be defined for i from 0 up to and including 27 resp. 23.
Finally, there is no need to define your own min function as there is one in the Swift standard library.
Putting it all together your code would be:
var angles: [Double] = []
for i: Double in stride(from: 0, through: 27, by: 1) {
angles.append(atan(pow(2, -i)))
}
var Kvalues: [Double] = []
for i: Double in stride(from: 0, through: 23, by: 1) {
Kvalues.append(1/sqrt(abs(Double(1) + pow(2,-2 * i))))
if i > 0 {
Kvalues[Kvalues.count - 1] *= Kvalues[Kvalues.count - 2]
}
}
func cordic(beta: Double, n: Int) -> [Double] {
var beta1 = beta
let Kn = Kvalues[min(n-1, Kvalues.count - 1)]
var v: [Double] = [1,0]
var poweroftwo: Double = 1
var angle = angles[0]
for j in 0 ..< n {
let sigma: Double = beta1 < 0 ? -1 : 1
let factor: Double = sigma * poweroftwo
v = [v[0] - v[1] * factor, v[1] + v[0] * factor]
beta1 -= sigma * angle
poweroftwo /= 2
angle = j + 1 >= angles.count ? angle / 2 : angles[j + 1]
}
return [v[0] * Kn, v[1] * Kn]
}
And that produces good approximations:
print(cordic(beta: Double.pi/9, n: 20)) // [0.93969210812600046, 0.34202155184390554]
print(cordic(beta: Double.pi/8, n: 20)) // [0.92388022188807306, 0.38268176805806309]
The exact values are
print(cos(Double.pi/9), sin(Double.pi/9)) // 0.939692620785908 0.342020143325669
print(cos(Double.pi/8), sin(Double.pi/8)) // 0.923879532511287 0.38268343236509
I have this error. I think I have two choices. Which one is best for my code? What do the differences mean?
cannot invoke 'RGBtoHSV' with an argument list of type '(Float,Float,Float)'
RGBtoHSV(CGFloat(r), CGFloat(g), CGFloat(b))
RGBtoHSV(CGFloat(), CGFloat(), CGFloat())
Also if you take a look at the screen shot if you could give me some pointers regarding the other couple of errors that would be great too. I know I have to match the types but I don't know the syntax order. http://i.imgur.com/sAckG6h.png
Thanks
func RGBtoHSV(r : CGFloat, g : CGFloat, b : CGFloat) -> (h : CGFloat, s : CGFloat, v : CGFloat) {
var h : CGFloat = 0.0
var s : CGFloat = 0.0
var v : CGFloat = 0.0
let col = UIColor(red: r, green: g, blue: b, alpha: 1.0)
col.getHue(&h, saturation: &s, brightness: &v, alpha: nil)
return (h, s, v)
}
// process the frame of video
func captureOutput(captureOutput:AVCaptureOutput, didOutputSampleBuffer sampleBuffer:CMSampleBuffer, fromConnection connection:AVCaptureConnection) {
// if we're paused don't do anything
if currentState == CurrentState.statePaused {
// reset our frame counter
self.validFrameCounter = 0
return
}
// this is the image buffer
var cvimgRef:CVImageBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)
// Lock the image buffer
CVPixelBufferLockBaseAddress(cvimgRef, 0)
// access the data
var width: size_t = CVPixelBufferGetWidth(cvimgRef)
var height:size_t = CVPixelBufferGetHeight(cvimgRef)
// get the raw image bytes
let buf = UnsafeMutablePointer<UInt8>(CVPixelBufferGetBaseAddress(cvimgRef))
var bprow: size_t = CVPixelBufferGetBytesPerRow(cvimgRef)
var r:Float = 0.0
var g:Float = 0.0
var b:Float = 0.0
for var y = 0; y < height; y++ {
for var x:UInt8 = 0; x < width * 4; x += 4 { // error: '<' cannot be applied to operands of type 'UInt8' and 'Int'
b += buf[x]
g += buf[x + 1]
r += buf[x + 2]
}
buf += bprow(UnsafeMutablePointer(UInt8)) // error: '+=' cannot be applied to operands of type 'UnsafeMutablePointer<UInt8>' and 'size_t'
}
r /= 255 * (width*height)
g /= 255 * (width*height)
b /= 255 * (width*height)
//}
// convert from rgb to hsv colourspace
var h:Float = 0.0
var s:Float = 0.0
var v:Float = 0.0
RGBtoHSV(r, g, b) // error
You have a lot of type mismatch error.
The type of x should not be UInt8 because x to increase until the value of the width.
for var x:UInt8 = 0; x < width * 4; x += 4 { // error: '<' cannot be applied to operands of type 'UInt8' and 'Int'
So fix it like below:
for var x = 0; x < width * 4; x += 4 {
To increment the pointer address, you can use advancedBy() function.
buf += bprow(UnsafeMutablePointer(UInt8)) // error: '+=' cannot be applied to operands of type 'UnsafeMutablePointer<UInt8>' and 'size_t'
Like below:
var pixel = buf.advancedBy(y * bprow)
And this line,
RGBtoHSV(r, g, b) // error
There are no implicit casts in Swift between CGFloat and Float unfortunately. So you should cast explicitly to CGFloat.
RGBtoHSV(CGFloat(r), g: CGFloat(g), b: CGFloat(b))
The whole edited code is here:
func RGBtoHSV(r: CGFloat, g: CGFloat, b: CGFloat) -> (h: CGFloat, s: CGFloat, v: CGFloat) {
var h: CGFloat = 0.0
var s: CGFloat = 0.0
var v: CGFloat = 0.0
let col = UIColor(red: r, green: g, blue: b, alpha: 1.0)
col.getHue(&h, saturation: &s, brightness: &v, alpha: nil)
return (h, s, v)
}
// process the frame of video
func captureOutput(captureOutput:AVCaptureOutput, didOutputSampleBuffer sampleBuffer:CMSampleBuffer, fromConnection connection:AVCaptureConnection) {
// if we're paused don't do anything
if currentState == CurrentState.statePaused {
// reset our frame counter
self.validFrameCounter = 0
return
}
// this is the image buffer
var cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer)
// Lock the image buffer
CVPixelBufferLockBaseAddress(cvimgRef, 0)
// access the data
var width = CVPixelBufferGetWidth(cvimgRef)
var height = CVPixelBufferGetHeight(cvimgRef)
// get the raw image bytes
let buf = UnsafeMutablePointer<UInt8>(CVPixelBufferGetBaseAddress(cvimgRef))
var bprow = CVPixelBufferGetBytesPerRow(cvimgRef)
var r: Float = 0.0
var g: Float = 0.0
var b: Float = 0.0
for var y = 0; y < height; y++ {
var pixel = buf.advancedBy(y * bprow)
for var x = 0; x < width * 4; x += 4 { // error: '<' cannot be applied to operands of type 'UInt8' and 'Int'
b += Float(pixel[x])
g += Float(pixel[x + 1])
r += Float(pixel[x + 2])
}
}
r /= 255 * Float(width * height)
g /= 255 * Float(width * height)
b /= 255 * Float(width * height)
//}
// convert from rgb to hsv colourspace
var h: Float = 0.0
var s: Float = 0.0
var v: Float = 0.0
RGBtoHSV(CGFloat(r), g: CGFloat(g), b: CGFloat(b)) // error
}