Detecting Shake Gesture by CoreMotion - ios7

I want to detect device shake using coreMotion framework for iOS 7. Can anyone help me out how to do this ?
I wrote code described below in my viewDidAppear
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 1.0/60.0;
__block double myAcceleration;
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *motion, NSError *error)
{
myAcceleration = motion.userAcceleration.y;
CATransform3D transform;
transform = CATransform3DMakeRotation(
motion.attitude.pitch, 1, 0, 0);
transform = CATransform3DRotate(transform,
motion.attitude.roll, 0, 1, 0);
transform = CATransform3DRotate(transform,
motion.attitude.yaw, 0, 0, 1);
}
];
but not detecting shake.

#define accelerationThreshold 0.30
- (void)motionMethod:(CMDeviceMotion *)deviceMotion
{
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
CMAcceleration userAcceleration = deviceMotion.userAcceleration;
if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold)
{
float sensitivity = 1;
float x1 = 0, x2 = 0, y1 = 0, y2 = 0, z1 = 0, z2 = 0;
double totalAccelerationInXY = sqrt(userAcceleration.x * userAcceleration.x +
userAcceleration.y * userAcceleration.y);
if (0.85 < totalAccelerationInXY < 3.45) {
x1 = userAcceleration.x;
y1 = userAcceleration.y;
z1 = userAcceleration.z;
float change = fabs(x1-x2+y1-y2+z1-z2);
if (sensitivity < change) {
// print change in position in coordinates.
NSLog (#"total=%f x=%f y=%f z=%f timeStamp:%f, UpTime:%f", totalAccelerationInXY, userAcceleration.x, userAcceleration.y, userAcceleration.z, deviceMotion.timestamp, [[NSProcessInfo processInfo] systemUptime]);
x2 = x1;
y2 = y1;
z2 = z1;
}
}
}
}

I know its old but i am posting for others who might be interested.
This Shake detection code can be also be used without ViewController or when the app is in the background (just by using the coremotion)
import CoreMotion
private var centralManager : CBCentralManager!
#IBOutlet weak var shakeLabel: UILabel!
let manager = CMMotionManager()
override func viewDidLoad() {
super.viewDidLoad()
var xInPositiveDirection = 0.0
var xInNegativeDirection = 0.0
var shakeCount = 0
var tempVariable = 0
manager.deviceMotionUpdateInterval = 0.02
manager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: {
(data, error) in
if data!.userAcceleration.x > 1.0 || data!.userAcceleration.x < -1.0 {
if data!.userAcceleration.x > 1.0 {
xInPositiveDirection = data!.userAcceleration.x
}
if data!.userAcceleration.x < -1.0 {
xInNegativeDirection = data!.userAcceleration.x
}
if xInPositiveDirection != 0.0 && xInNegativeDirection != 0.0 {
shakeCount = shakeCount + 1
xInPositiveDirection = 0.0
xInNegativeDirection = 0.0
}
if shakeCount > 5 {
tempVariable = tempVariable + 1
self.shakeLabel.text = "Shaken! \(tempVariable)"
shakeCount = 0
}
}
})

Related

Assignment problem - What am I doing wrong?

I am working on image seam carving project, looking for some help. Can someone tell me what am I doing wrong here, Hyperskill is not accepting my solution. I am pretty sure I did not understand the project statement correctly. (I’ve been fighting it for a week)
Project: https://hyperskill.org/projects/100/stages/553/implement
First I am finding the minimum seam from all possible seams.
var minX = 0
// (minSeamX, 0) would be the coordinate of the minimum seam
var minSeamX = 0
var minSeam = Double.MAX_VALUE
//Starting from top left find the sum of pixel energies for all possible seams(#width number of possible seams)
for (column in 0 until width) {
var totalSeam = 0.0
var xHigh = column
var xLow = column
var min = Double.MAX_VALUE
for (y in 0 until height) {
for (x in xLow..xHigh) {
if (x < 0 || x > width - 1) continue
val energy = calculateEnergy(x, y, bufferedImage)
// println("Energy $x $y $energy")
if (energy < min) {
min = energy
minX = x
}
}
totalSeam += min
min = Double.MAX_VALUE
xLow = minX - 1
xHigh = minX + 1
}
if (totalSeam < minSeam) {
minSeamX = column
minSeam = totalSeam
}
println("total:$totalSeam")
}
after that I am applying the color to the minimum seam pixels
var xLow = minSeamX
var xHigh = minSeamX
var min = Double.MAX_VALUE
for (y in 0 until height) {
for (x in xLow..xHigh) {
val energy = calculateEnergy(x, y, bufferedImage)
if (energy < min) {
min = energy
minX = x
}
}
val createGraphics = applyColor(outImage, minX, y)
min = Double.MAX_VALUE
xLow = minX - 1
xHigh = minX + 1
}
Complete code
package seamcarving
import java.awt.Color
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import kotlin.math.pow
import kotlin.math.sqrt
fun main(args: Array<String>) {
val bufferedImage = ImageIO.read(File("/Users/name/Downloads/images/blue.png"))
val outImage = ImageIO.read(File("/Users/name/Downloads/images/blue.png"))
val height = bufferedImage.height
val width = bufferedImage.width
var minX = 0
// (minSeamX, 0) would be the coordinate of the minimum seam
var minSeamX = 0
var minSeam = Double.MAX_VALUE
//Starting from top left find the sum of pixel energies for all possible seams(#width number of possible seams)
for (column in 0 until width) {
var totalSeam = 0.0
var xHigh = column
var xLow = column
var min = Double.MAX_VALUE
for (y in 0 until height) {
for (x in xLow..xHigh) {
if (x < 0 || x > width - 1) continue
val energy = calculateEnergy(x, y, bufferedImage)
// println("Energy $x $y $energy")
if (energy < min) {
min = energy
minX = x
}
}
totalSeam += min
min = Double.MAX_VALUE
xLow = minX - 1
xHigh = minX + 1
}
if (totalSeam < minSeam) {
minSeamX = column
minSeam = totalSeam
}
println("total:$totalSeam")
}
var xLow = minSeamX
var xHigh = minSeamX
var min = Double.MAX_VALUE
for (y in 0 until height) {
for (x in xLow..xHigh) {
val energy = calculateEnergy(x, y, bufferedImage)
if (energy < min) {
min = energy
minX = x
}
}
val createGraphics = applyColor(outImage, minX, y)
min = Double.MAX_VALUE
xLow = minX - 1
xHigh = minX + 1
}
// for (x in 0 until width) {
// for (y in 0 until height) {
// val intensity = ((255.0 * array[x][y]) / max).toInt()
// val color = Color(intensity, intensity, intensity)
//// outputImage.setRGB(x, y, intensity)
// createGraphics.paint = color
// createGraphics.fillRect(x, y, 1, 1)
// }
// }
ImageIO.write(outImage, "png", File("out.png"))
// ImageIO.write(bufferedImage, "png", File("${args[3]}"))
}
private fun applyColor(outputImage: BufferedImage, maxX: Int, maxY: Int): Graphics2D? {
val createGraphics = outputImage.createGraphics()
val color = Color(255, 0, 0)
createGraphics.paint = color
createGraphics.fillRect(maxX, maxY, 1, 1)
return createGraphics
}
private fun calculateEnergy(x: Int, y: Int, bufferedImage: BufferedImage): Double {
return sqrt(getXGradient(x, y, bufferedImage) + getYGradient(x, y, bufferedImage))
}
fun getXGradient(x: Int, y: Int, inImage: BufferedImage): Double {
val width = inImage.width
var xx = x
var yy = y
if (x == 0) xx = 1
if (x == width - 1) xx = x - 1
val lc = Color(inImage.getRGB(xx - 1, yy))
val rc = Color(inImage.getRGB(xx + 1, yy))
return (lc.red - rc.red).toDouble().pow(2.0) + (lc.green - rc.green).toDouble().pow(2.0) + (lc.blue - rc.blue).toDouble().pow(2.0)
}
fun getYGradient(x: Int, y: Int, inImage: BufferedImage): Double {
val height = inImage.height
var xx = x
var yy = y
if (y == 0) yy = 1
if (y == height - 1) yy = y - 1
val lc = Color(inImage.getRGB(xx, yy - 1))
val rc = Color(inImage.getRGB(xx, yy + 1))
return (lc.red - rc.red).toDouble().pow(2.0) + (lc.green - rc.green).toDouble().pow(2.0) + (lc.blue - rc.blue).toDouble().pow(2.0)
}

Binary operator '+=' cannot be applied to operands of type 'Int' and 'UInt8'

Translating Obj-C to Swift. As you can see I declared let buf = UnsafeMutablePointer<UInt8>(CVPixelBufferGetBaseAddress(cvimgRef)) so I'm getting the error in the for loop below it.
Binary operator '+=' cannot be applied to operands of type 'Int' and 'UInt8'
Also as a little addendum I don't know how to translate the remaining Obj-C code below the for loop. What does that slash mean and how do I deal with the pointer? I have to say UnsafeMutableFloat somewhere?
// 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 = 0
var g = 0
var b = 0
for var y = 0; y < height; y++ {
for var x = 0; x < width * 4; x += 4 {
b += buf[x]; g += buf[x + 1]; r += buf[x + 2] // error
}
buf += bprow() // error
}
Remaining Obj-C code.
r/=255*(float) (width*height);
g/=255*(float) (width*height);
b/=255*(float) (width*height);
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
}

Using libSVM programmatically

I have started using libSVM (java: https://github.com/cjlin1/libsvm) programmatically. I wrote the following code to test it:
svm_parameter param = new svm_parameter();
// default values
param.svm_type = svm_parameter.C_SVC;
param.kernel_type = svm_parameter.RBF;
param.degree = 3;
param.gamma = 0;
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 40;
param.C = 1;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.probability = 0;
param.nr_weight = 0;
param.weight_label = new int[0];
param.weight = new double[0];
svm_problem prob = new svm_problem();
prob.l = 4;
prob.y = new double[prob.l];
prob.x = new svm_node[prob.l][2];
for(int i = 0; i < prob.l; i++)
{
prob.x[i][0] = new svm_node();
prob.x[i][1] = new svm_node();
prob.x[i][0].index = 1;
prob.x[i][1].index = 2;
prob.x[i][0].value = (i%2!=0)?-1:1;
prob.x[i][1].value = (i/2%2==0)?-1:1;
prob.y[i] = (prob.x[i][0].value == 1 && prob.x[i][1].value == 1)?1:-1;
System.out.println("X = [ " + prob.x[i][0].value + ", " + prob.x[i][1].value + " ] \t -> " + prob.y[i] );
}
svm_model model = svm.svm_train(prob, param);
int test_length = 4;
for( int i = 0; i < test_length; i++)
{
svm_node[] x_test = new svm_node[2];
x_test[0] = new svm_node();
x_test[1] = new svm_node();
x_test[0].index = 1;
x_test[0].value = (i%2!=0)?-1:1;
x_test[1].index = 2;
x_test[1].value = (i/2%2==0)?-1:1;
double d = svm.svm_predict(model, x_test);
System.out.println("X[0] = " + x_test[0].value + " X[1] = " + x_test[1].value + "\t\t\t Y = "
+ ((x_test[0].value == 1 && x_test[1].value == 1)?1:-1) + "\t\t\t The predicton = " + d);
}
Since I am testing on the same training data, I'd expect to get 100% accuracy, but the output that I get, is the following:
X = [ 1.0, -1.0 ] -> -1.0
X = [ -1.0, -1.0 ] -> -1.0
X = [ 1.0, 1.0 ] -> 1.0
X = [ -1.0, 1.0 ] -> -1.0
*
optimization finished, #iter = 1
nu = 0.5
obj = -20000.0, rho = 1.0
nSV = 2, nBSV = 2
Total nSV = 2
X[0] = 1.0 X[1] = -1.0 Y = -1 The predicton = -1.0
X[0] = -1.0 X[1] = -1.0 Y = -1 The predicton = -1.0
X[0] = 1.0 X[1] = 1.0 Y = 1 The predicton = -1.0
X[0] = -1.0 X[1] = 1.0 Y = -1 The predicton = -1.0
We can see that the following prediction is erroneous:
X[0] = 1.0 X[1] = 1.0 Y = 1 The predicton = -1.0
Anyone knows what is the mistake in my code?
You're using Radial Basis Function (param.kernel_type = svm_parameter.RBF) which uses gamma. Setting 'param.gamma = 1' should yield 100% accuracy.

Convert an image into polaroid filter

I am trying to convert an image into polaroid filter in the following way:
int m_width = self.size.width;
int m_height = self.size.height;
uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(
rgbImage,
m_width,
m_height,
8,
m_width * 4,
colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast
);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [self CGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// now convert to grayscale
uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
for(int y = 0; y < m_height; y++)
{
for(int x = 0; x < m_width; x++)
{
uint32_t rgbPixel=rgbImage[y*m_width+x];
uint32_t redIn = 0, greenIn = 0,blueIn = 0,max = 0,min = 0,chroma = 0,hue = 0,saturation = 0,redOut = 0,greenOut = 0,blueOut = 0;
redIn = (rgbPixel>>24)&255;
greenIn = (rgbPixel>>16)&255;
blueIn = (rgbPixel>>8)&255;
//NSLog(#"redIn %u greenIn %u blueIn %u",redIn,greenIn,blueIn);
max = redIn > greenIn ? redIn > blueIn ? redIn : blueIn : greenIn > blueIn ? greenIn : blueIn;
min = redIn < greenIn ? redIn < blueIn ? redIn : blueIn : greenIn < blueIn ? greenIn : blueIn;
chroma = max - min;
if(chroma != 0)
{
if(max == redIn)
hue = ((greenIn - blueIn) / chroma) %6 ;
else if(max == greenIn)
hue = ((blueIn - redIn)/chroma) + 2;
else if(max == blueIn)
hue = ((redIn - greenIn)/chroma) + 4;
hue = (hue+20)%6;
if(chroma != 0)
saturation = chroma / max;
if(hue >= 0 && hue < 1)
{
redOut = chroma + max - chroma;
greenOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
blueOut = 0 + max - chroma;
}
else if(hue >= 1 && hue < 2)
{
redOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
greenOut = chroma + max - chroma;
blueOut = 0 + max - chroma;
}
else if(hue >= 2 && hue < 3)
{
redOut = 0 + max - chroma;
greenOut = chroma + max - chroma;
blueOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
}
else if(hue >= 3 && hue < 4)
{
redOut = 0 + max - chroma;
greenOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
blueOut = chroma + max - chroma;
}
else if(hue >= 4 && hue < 5)
{
redOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
greenOut = 0 + max - chroma;
blueOut = chroma + max - chroma;
}
else if(hue >= 5 && hue < 6)
{
redOut = chroma + max - chroma;
greenOut = 0 + max - chroma;
blueOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
}
}
//NSLog(#"redOut %u greenOut %u blueOut %u",redOut,greenOut,blueOut);
m_imageData[y*m_width+x]=redOut + greenOut + blueOut;
}
}
free(rgbImage);
// convert from a gray scale image back into a UIImage
uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1);
// process the image back to rgb
for(int i = 0; i < m_height * m_width; i++)
{
result[i*4]=0;
int val=m_imageData[i];
result[i*4+1]=val;
result[i*4+2]=val;
result[i*4+3]=val;
}
free(m_imageData);
// create a UIImage
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(result,
m_width,
m_height,
8,
m_width * sizeof(uint32_t),
colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast
);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
#ifdef kNYXReturnRetainedObjects
UIImage* resultUIImage = [[UIImage alloc] initWithCGImage:image];
#else
UIImage *resultUIImage = [UIImage imageWithCGImage:image];
#endif
CGImageRelease(image);
free(result);
return resultUIImage;
but I'm not getting the polaroid image after execution this code.
What's the problem in this code or can you suggest me some other way.
The most obvious problem with the code is that, at the end of the method, you create a drawing context, do nothing in it, then take an image from it and return that as a UIImage. This will be blank, presumably. I think you're trying to obtain the image that is held in the data in result - I think you need to look at CGImageCreate instead rather than setting up a new context.
See here for information on creating a data provider with raw data, which you will need to pass to CGImageCreate. This all does look rather complicated.

how do I draw an arrow onto my view?

Hi there and thank you in advance for any responses.
I want to draw an big wide arrow on my UIView that has a light green to dark green shade.
I have been looking at documentation for CGContextRef,CGPathRef and UIBezierPath but i'm just getting more confused.
Could someone please help me with this.thanks!
Question is old but I'm pleased to share you some code that I hope usefull - Swift 2.3 compatible -
public extension UIView {
public enum PeakSide: Int {
case Top
case Left
case Right
case Bottom
}
public func addPikeOnView(side side: PeakSide, size: CGFloat = 10.0) {
self.layoutIfNeeded()
let peakLayer = CAShapeLayer()
var path: CGPathRef?
switch side {
case .Top:
path = self.makePeakPathWithRect(self.bounds, topSize: size, rightSize: 0.0, bottomSize: 0.0, leftSize: 0.0)
case .Left:
path = self.makePeakPathWithRect(self.bounds, topSize: 0.0, rightSize: 0.0, bottomSize: 0.0, leftSize: size)
case .Right:
path = self.makePeakPathWithRect(self.bounds, topSize: 0.0, rightSize: size, bottomSize: 0.0, leftSize: 0.0)
case .Bottom:
path = self.makePeakPathWithRect(self.bounds, topSize: 0.0, rightSize: 0.0, bottomSize: size, leftSize: 0.0)
}
peakLayer.path = path
let color = (self.backgroundColor ?? .clearColor()).CGColor
peakLayer.fillColor = color
peakLayer.strokeColor = color
peakLayer.lineWidth = 1
peakLayer.position = CGPoint.zero
self.layer.insertSublayer(peakLayer, atIndex: 0)
}
func makePeakPathWithRect(rect: CGRect, topSize ts: CGFloat, rightSize rs: CGFloat, bottomSize bs: CGFloat, leftSize ls: CGFloat) -> CGPathRef {
// P3
// / \
// P1 -------- P2 P4 -------- P5
// | |
// | |
// P16 P6
// / \
// P15 P7
// \ /
// P14 P8
// | |
// | |
// P13 ------ P12 P10 -------- P9
// \ /
// P11
let centerX = rect.width / 2
let centerY = rect.height / 2
var h: CGFloat = 0
let path = CGPathCreateMutable()
var points: [CGPoint] = []
// P1
points.append(CGPointMake(rect.origin.x, rect.origin.y))
// Points for top side
if ts > 0 {
h = ts * sqrt(3.0) / 2
let x = rect.origin.x + centerX
let y = rect.origin.y
points.append(CGPointMake(x - ts, y))
points.append(CGPointMake(x, y - h))
points.append(CGPointMake(x + ts, y))
}
// P5
points.append(CGPointMake(rect.origin.x + rect.width, rect.origin.y))
// Points for right side
if rs > 0 {
h = rs * sqrt(3.0) / 2
let x = rect.origin.x + rect.width
let y = rect.origin.y + centerY
points.append(CGPointMake(x, y - rs))
points.append(CGPointMake(x + h, y))
points.append(CGPointMake(x, y + rs))
}
// P9
points.append(CGPointMake(rect.origin.x + rect.width, rect.origin.y + rect.height))
// Point for bottom side
if bs > 0 {
h = bs * sqrt(3.0) / 2
let x = rect.origin.x + centerX
let y = rect.origin.y + rect.height
points.append(CGPointMake(x + bs, y))
points.append(CGPointMake(x, y + h))
points.append(CGPointMake(x - bs, y))
}
// P13
points.append(CGPointMake(rect.origin.x, rect.origin.y + rect.height))
// Point for left side
if ls > 0 {
h = ls * sqrt(3.0) / 2
let x = rect.origin.x
let y = rect.origin.y + centerY
points.append(CGPointMake(x, y + ls))
points.append(CGPointMake(x - h, y))
points.append(CGPointMake(x, y - ls))
}
let startPoint = points.removeFirst()
self.startPath(path: path, onPoint: startPoint)
for point in points {
self.addPoint(point, toPath: path)
}
self.addPoint(startPoint, toPath: path)
return path
}
private func startPath(path path: CGMutablePath, onPoint point: CGPoint) {
CGPathMoveToPoint(path, nil, point.x, point.y)
}
private func addPoint(point: CGPoint, toPath path: CGMutablePath) {
CGPathAddLineToPoint(path, nil, point.x, point.y)
}
}
In this way you can call this for every kind of view:
let view = UIView(frame: frame)
view.addPikeOnView(side: .Top)
In a future I'll add offset for pike position.
yes, names are definitely improvable!