GetLeft of a Path in a canvas - path

I am trying to find the Left of a Path using Canvas.GetLeft(aPath);
But i am getting a 0.
I have tried calling aPath.Measure and got the DesiredSize successfully which has the width but is there any way i could get the left of a aPath.
Please help.
Thanks
N

I was having a similar issue and found that using the VisualTreeHelper.GetOffset() worked:
Vector offset = VisualTreeHelper.GetOffset(myPath);
//moving the path around the canvus just a bit
Canvas.SetTop(myPath, offset.X + 100);
Canvas.SetLeft(myPath, offset.Y + 100);
Good luck!

Related

Random place in frame

I am developing a small game but I'm stuck on this. I want to generate a random position for an SKSpriteNode called div. I have this code
let random = arc4random() % UInt32(self.frame.size.width)
let random1 = arc4random() % UInt32(self.frame.size.height)
div.position = CGPoint(x: Int(random), y: Int(random1))
self.addChild(div)
I can't figure out what's wrong and why the div is sometimes out of the frame and not visible. Does anyone has a good solution for this?
Any help is appreciated! Thanks!
try setting this in your didMoveToView,
self.size = view.bounds.size

Center of rotation for cc.RotateBy applied on a cc.DrawNode in cocos2d-js

I made a drawNode to draw primitive using this code:
var.drawNode = cc.DrawNode.create();
drawNode.drawSegment(this.pos, cc.p(this.pos.x + this.length * Math.sin(this.rotation), this.pos.y + this.length * Math.cos(this.rotation)), STICK_THICKESS, cc.color(255,255,0,255));
It basically draws a line from this.pos to another point.
Now I want to rotate the line around this.pos, so I thought I just need to simply add this:
drawNode.setAnchorPoint(this.pos);
var rotate = cc.RotateBy.create(2, 360);
drawNode.runAction(rotate);
But it's still rotating around some random point.
Ugly but working method:
drawNode.setContentSize(1, 1);
drawNode.setAnchorPoint(this.pos);
drawNode.setPosition(this.pos);
var rotate = cc.RotateBy.create(2, 360);
drawNode.runAction(rotate);
BTW create methods are deprecated in Cocos2d-html5 3.0+. Use cc.rotateBy() instead of cc.RotateBy.Create(), new cc.DrawNode() instead of cc.DrawNode.create()

Too many arguments to method call error when using sizeWithAtrributes in ios 7 iPhone

Previously in my application i was using sizeWithFont like this.
Im trying to adjust the size and position of an image according to the text label width and position. so i try i like this,
CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithFont:self.categoryLabel.font].width + 10, y, _imageSize.width, _imageSize.height);
But in iOS 7 it is deprecated and asked to sizeWithAttributes.
When i tried like this i was getting the "x" cordinate like this.
But when i use the code like this CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:#{NSFontAttributeName:[UIFont systemFontOfSize:self.categoryLabel.frame.size.width+10]}],y,_imageSize.width,_imageSize.height);
im getting the "x" cordinate like this.
Can anyone please help me on this.
Replace this code:
self.categoryLabel.font.width+10
with
self.categoryLabel.frame.size.width+10
There is no property width on font. I assume you want to get width from frame of your label.
// EXTENDED
This is an example of whole rect creation line, CGRect expect 4 parameters, I listed it line by line separated by coma, I'm not sure is it what you need but it will give you some result:
CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:#{NSFontAttributeName:[UIFont systemFontOfSize:self.categoryLabel.font.pointSize]}].width, //<- this .width takes width ot the size with font but maybe you required height
y,
_imageSize.width,
_imageSize.height);
If you have a look on the line below (your old one) there is not 4 parameters:
CGRect rect = CGRectMake(_frame.origin.x + [self.categoryLabel.text sizeWithAttributes:#{NSFontAttributeName:[UIFont systemFontOfSize:[self.categoryLabel.frame.size.width]+10,y,_imageSize.width,_im‌​ageSize.height]}];
it looks like it's just one so it won't work.

Is there a shortcut for (self.frame.origin.y + self.frame.size.height)?

I write too much of this code :
self.frame.origin.y + self.frame.size.height
Is there shortcut for this? Something like self.frame.y_plus_height?
If there is, I'm not sure if that good news or bad news for all the times I wrote the full sentence.
This should do the trick:
CGFloat res = CGRectGetMaxY(self.frame);
Documentation can be found here.
EDIT: according to explanation from rob mayoff (see comments) this is a little more expensive than simply summing origin.y + size.height in code.
There's a great category on UIView created by nfarina.
Check it out here.
Using this category, you can get the "max Y" or "bottom" of a view like this (where self is a view):
CGFloat maxY = self.$bottom;
This category is really awesome because it enables you set to frame properties easily, too. In example, to move the view over to the right 3 points, you can do this:
self.$x += 3.0f;
I realize this is asking for Objective-C, but for those using Swift and stumbling upon this question, you can just create a CGRect extension:
extension CGRect {
func maxYinParentFrame() -> CGFloat {
return self.origin.y + self.size.height
}
}
You can then use it anywhere.
let mySize = myView.frame.maxYinParentFrame()

OpenCV: How to get corners of CvBox2D?

I need to find corner positions of CvBox2D (or MCvBox2D) to map found contours on game object in XNA. I have a problem with correct translation of rotation angle. I thought that this is kind of basic operation but I kind find any solution in Internet.
I tried:
rotationAngle = box.angle * (180.0/ CV_PI);
angle = box.angle;
box.angle=rotationAngle;
alien.X = box.center.X - box.Width / 2;
alien.Y = box.center.Y - box.Height / 2;
alien.angle=angle;
but it wasn't translating it correctly.
Had someone ever tried to get corners on this kind of structure?
In EmguCV you just need to call
PointF[] corners = box.GetVertices();
if box is a MCvBox2D.
The simplest way to get the vertices of a CvBox2D is to convert it to a RotatedRect:
CvBox2D box = ...
cv::RotatedRect rr(box);
cv::Point2f vertices[4];
rr.points(vertices);
// vertices now has the four corners your seek

Resources