Accessing the position of simple shapes - bonsaijs

I have a Rect assigned to a variable myrect:
var myrect = new Rect (250,0,20,200);
I thought that writing:
console.log(myrect.x);
would output 250 but, it says "undefined".
I would like to know how I can redraw this rectangle by performing
arithmetic on its x and y coordinates.

The way to access the x and y properties is through the attr() method.
console.log(myrect.attr('x'));
See it in the documentation. But I have to admit, the documentation isn't that beginner-friendly yet.

Related

Correct way of extending map boundaries without using bounds.extend(point)

I am using map bounds to display markers that fall within the current viewport. Upon zooming or panning the bounds are recalculated and the markers that fall within these bounds are redrawn, effectively hiding any not contained within the viewport.
I would like to do it so that the markers draw slightly out of the current viewport, however this would involve extending the bounds equally from all sides rather than using bounds.extend(point). Is this possible?
//I would like to extend this value in order to draw features that are slightly off the viewport
var bounds = map.getBounds()
//This is how I am currently extending the bounds, it works but I am unsure if it is the correct way.
bounds.b.b = bounds.b.b - 0.5
bounds.b.f = bounds.b.f + 0.5
bounds.f.b = bounds.f.b - 0.5
bounds.f.f = bounds.f.f + 0.5
//Determining whether the feature lies within the current viewport
var result = bounds.contains(Featurecenter)
center = null
//If the feature lies within the viewport
if (result) {
Feature.setMap(map) //Making the feature visible on the map
}
Don't use b, f and such, which are undocumented properties, instead use documented methods such as getNorthEast(), getSouthWest() then lat() and lng() when you need to extract coordinates from a bounds object.
I don't know of any other method to extend a bounds object than to pass new coordinates to it (see below).
The LatLngBounds object has an extend() method that you must use to achieve what you want.
We don't know of "how much" you need to extend the bounds; depending on that, and on the current zoom level when you want to extend your bounds, you will probably need to do some maths.
If you need to extend your bounds by a given (and constant) distance, whatever zoom level is currently set, I suggest you to read some other Q/A that explain how to do that. For example:
Google maps distance based on the zoom level
Google Maps V3 - How to calculate the zoom level for a given bounds

How to specify symbol position on the marker?

I am using Qwt library. I have added vertical QwtPlotMarker on the plot. I want to add QwtSymbol to my marker and I want the symbol to be in the bottom of the marker. When I use setSymbol() it places symbol in the middle of the marker. There are setLabelAlignment() function to specify where the label is drawn. Unfortunately, I did not found anything like that for symbol. I have read Qwt User's Guide where I found: The setSymbol() member assigns a symbol to the marker. The symbol is drawn at the specified point. So, how to specify that point?
It is a very old question, but I found solution and I want to share with you.
There is way to specify this point. Method setValue(double x, double y) do this, but this method not describes in the documentation (it's very strange for me), but this method works!
I wrote small but useful snippet. If I understand you correctly, this code do exactly what you need
QwtSymbol *sym=new QwtSymbol(QwtSymbol::Diamond,QBrush(Qt::red),QPen(Qt::red),QSize(5,5));
//create the symbol
QwtPlotMarker *mark=new QwtPlotMarker;
mark->setSymbol(sym);
mark->setLineStyle(QwtPlotMarker::VLine);
//set vertical line
mark->setValue(5,ui->qwtPlot->axisInterval(QwtPlot::yLeft).minValue());
//here you have to set the coordinate axis i.e. where the axis are meeting.
//as you can see I set coordinate 5 and the lowest coordinate of Y axis
//you can set here needed coordinates
mark->attach(ui->qwtPlot);
I hope, it helps.

How to Find global position of objects in a rotating scene THREE.JS

I am working on a 3D mesh manipulator using this : http://leapmotion.com. So far, I have been able manipulate the points just fine, by 'grabbing' and moving them, however I now want to be able to rotate the mesh and work on the opposite face. What I have done is add an extra object that is called 'rotatable' as Shown below:
scene=new THREE.Scene();
camera = new THREE.PerspectiveCamera(70,window.innerWidth/window.innerHeight,1,8000)
renderer=new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, maxLights:5 } )
//This is the 'Mesh Scene'
rotatable = new THREE.Object3D()
scene.add(rotatable)
//Mesh we are altering
var material = new THREE.MeshNormalMaterial()
material.side=2
var geom = new THREE.SphereGeometry(200,10,10);
var sphere = new THREE.Mesh(geom, material)
rotatable.add(sphere)
I am then trying to change the vertices of this sphere, but to do so I need to do a 'collision test' in order to see if the vertex is being 'grabbed' This involves check the vertex position and see if it coincides with one of your finger position (psuedoCode below)
if(finger.x == vertex.x && finger.y == vertex.y && finger.z == vertex.z){
vertex.grabbed = true
}
This works fine when the rotatable's rotation is zero, however when it starts to rotate, the collision test will still be testing for the unrotated vertex position (which makes sense). My question is how to find the position of the vertex in its 'scene / global' position. The only way I can think of doing this so far is to calculate the rotation of the 'rotatable' and use this vector to calculate the new vertex position.
I know nothing about math, so this may not be the way to go, and even if it is I will have to struggle through it so hard that I won't ever know if I'm just doing the math incorrectly, or this isn't the way I should go about calculating it. Obviously I'm willing to go through this work, but just want to make sure this is the way to do it, rather then an other simpler method.
If there are any other questions about the code, please let me know, and Thanks in advance for your time!
Isaac
To get the world position of a vertex specified in local coordinates, apply the object's world transform to the vertex like so:
vertex.applyMatrix4( object.matrixWorld );
(I am not familiar with leapmotion, so hopefully it does not impact this answer.)
Tip: maxLights is no longer required. And it is best to avoid material.side = 2. Use material.side = THREE.DoubleSide instead.
You can find the constants here: https://github.com/mrdoob/three.js/blob/master/src/Three.js
three.js r.55

XNA - check if Vector2 is inside Rectangle

What's the best method to check if a Vector2 is inside a Rectangle?
myRect.contains(myVector2) does not work, because it expects a Point or a Rectangle.
I know I could cast the the Vector2 to a Point or even to a Rectangle with a size of 1/1, but I am unsure about performance.
What are your experiences?
One of the overloaded versions of Contains for the Rectangle struct, takes an X and Y coordinate.
Rectangle rect = new Rectangle(-5, -5, 10, 10);
Vector2 myVector2 = Vector2.Zero;
rect.Contains((int)myVector2.X, (int)myVector2.Y)
I assume you're talking about myVector2 representing a point rather than a distance or direction.
While there may be additional execution time in casting vs creating a point like Point(myVector2.X, myVector2.Y), the implications are most likely too small for you to ever notice (generally should avoid premature optimization)
Go with whatever looks cleanest and easier to maintain. If it were me I'd probably just create the new point in the method call...
You could also write an extension method.
public static Point Origin(this Vector2 v)
{
//original proposal
//return new Point( (int)v.X, (int)v.Y );
//better! does correctly round the values
return new Point( Convert.ToInt32(v.X), convert.ToInt32(v.Y));
}
Then you could do something like this rect.Contains(vec.Origin)
Something to remember, though: this method will not actually check if the vector is contained in the rectangle, this will only check if the origin is. Remember, a vector is direction and magnitude.

Is there a way to make drawText() update a QPicture's bounding rect?

Drawing on a QPicture should update its bounding rect. Like this:
>>> picture = QPicture()
>>> painter = QPainter(picture)
>>> picture.boundingRect()
QRect(0,0,0,0)
>>> painter.drawRect(20,20,50,50)
>>> picture.boundingRect()
QRect(20,20,50,50)
But if I draw text on it, the bounding rect isn't updated:
>>> picture = QPicture()
>>> painter = QPainter(picture)
>>> picture.boundingRect()
QRect(0,0,0,0)
>>> painter.drawText(10,10, "Hello, World!")
>>> picture.boundingRect()
QRect(0,0,0,0)
Obviously, it doesn't update the bounding rect.
Is there a way to make it repsect drawn text or do I have to do it manually? (Not too hard, but I hope that Qt can assist me here.)
Take a look at these overload methods, where you must specify the Bounding Rectangle after the text parameter (which is apparently different than the rectangle in the first argument's position):
Draws the given text within the
provided rectangle according to the
specified flags. The boundingRect (if
not null) is set to the what the
bounding rectangle should be in order
to enclose the whole text.
QPainter.drawText (1), QPainter.drawText (2)
Update:
It appears if you want to generate a bounding rectangle for the drawText() method in advance, you just call the boundingRect() method on QPainter, which does the following:
Returns the bounding rectangle of the
text as it will appear when drawn
inside the given rectangle with the
specified flags using the currently
set font(); i.e the function tells you
where the drawText() function will
draw when given the same arguments.
If the text does not fit within the
given rectangle using the specified
flags, the function returns the
required rectangle.
QPainter.boundingRect
I linked to BoundingRect with QRectF output, but the information applies to the other versions as well.
So basically, pass the result of QPainter.boundingRect() into the boundingRect parameter of the QPainter.drawText() method (the second QRect argument).
Update 2:
I APOLOGIZE PROFUSELY for being so damn dense. I forgot that drawText works differently in PyQt than in Qt. The bounding rectangle is RETURNED by the drawText function (not passed in like Qt) and in addition, you have to specify alignment flags before you get a bounding rectangle given back to you. (I even included the p.end() as per Aaron Digulla's comment):
pic = Qt.QPicture()
p = QtGui.QPainter(pic)
brect = p.drawText(10,10,200,200, QtCore.Qt.AlignCenter, "blah")
p.end()
print brect
print pic.boundingRect()
Here is the output:
PyQt4.QtCore.QRect(100, 103, 20, 14)
PyQt4.QtCore.QRect(0, 0, 0, 0)
So it appears you will have to set the bounding rectangle yourself, though at least it is returned to you by the output of the drawText() method when passing in flags.
This does not seem like ideal behaviour, that you would have to set the bounding rectangle yourself. I hope someone else has the answer you're looking for, but I suspect you may want to report this bug.
Painting doesn't change the size of something in Qt. The main reason is this:
A component has to paint itself
The paint triggers a resize
The resize triggers painting -> endless loop
So the resize has to happen during the layout phase. After that, the bounds should not change.
To solve your problem, use QFontMetric to figure out how big your text is going to be during or close to the construction of your picture and then resize it accordingly.
[EDIT] Hm ... try to call end() before requesting the bounding rect. If that works, you've found a bug (can't see a reason why the bounding rect should not exist as you add elements...)

Resources