Corona SDK: Rotating an object with specific reference point consecutively - object

I'm trying to make a square move by rotating it sideways with its reference point set to the bottom side of the direction it is moving to.
For example: if I would move the square to the right, I would set its reference point to the bottom right of it and animate a rotation of 90 degrees, after the movement is done, I increase the square X by the width of it and centrally set its rotation to 90 degrees (so that I can keep track of its position)
The thing is, how should I proceed to keep repeating it? 'cause if I try to rotate by another 90 degrees using bottom right reference point, it won't be using the right position. What should I do to get the new bottom right relative position?
Thanks!

Create a transparent image twice as wide and high as the square.
Place the square in the top left corner.
Rotating the image around its center will now rotate the square in the way you describe.

You will need some maths to track where the new bottom is, using some simple formula for 2D rotation, so you calculate BEFORE rotating, where the point you wish to modify.
Then you use xReference and yReference variables :)

Related

How to calculate what percentage of a pixel is within the bounds of a shape

I have a 2d grid where pixel centers are at the intersection of two half-grid lines, as shown below.
I also have a shape that is drawn on this grid. In my case the shape is a glyph, and is described by segments. Each segment has a start point, end point and a number of off-curve points. These segments can be quadratic curves or lines. What's important is that I can know the points and functions that make up the outline of the shape.
The rule for deciding which pixels should be turned on is simple: if the center of the pixel falls within the shape outline, turn that pixel on. The following image shows an example of applying this rule.
Now the problem I'm facing has to do with anti aliasing. What I'd like to do is to calculate what percentage of the area of a given pixel falls within the outline. As an example, in the image above, I've drawn a red square around a pixel that would be about 15% inside the shape.
The purpose of this would be so that I can then turn that pixel on only by 15% and thus get some cleaner edges for the final raster image.
While I was able to find algorithms for determining if a given point falls within a polygon (ray casting), I wasn't able to find anything about this type of problem.
Can someone can point me toward some algorithms to achieve this? Also let me know if I'm going about this problem in the wrong way!
This sounds like an X, Y problem.
You are asking for a way to calculate the perecentage of pixel coverage, but based on your question, it sounds that what you want to do is anti alias a polygon.
If you are working only with single color 2D shapes (i.e red, blue, magenta... squares, lines, curves...) A very simple solution is to create your image and blur the result afterwards.
This will automatically give you a smooth outline and is simple to implement in many languages.

Three.js calculate .obj size (bottom and top position.y)

How can I calculate object bottom position?
I created an object and a plane, both in (0,0,0) position, but the model geometrical center is placed in that point, not the bottom face of it, in result, the plane is clipping the half of it, see the demo: http://www.edar.com.pl/test-3d-3/
I'd like my models to have the same height when I place them on the screen. Also I want them all to intersect the shadow plane with their bottom parts, so I guess I have to calculate their heights and move them via object.position.y = obj_height/2; ?
Check Three.GeometryUtils.Center() - function which centers the geometry of an object.
So you could copy and modify the function to your liking to center the geometry at the bottom center.
Also you could also iterate over all vertices and just add or remove an offset-vector.

Gaps Between Rectangles Connected at the Center

I'm experimenting with a vector based graphics style with objects represented as series of line segments with a given width(it would probably be easier to think of these as rectangles). The problem is that these segments are connected at the center and leave a gap (shown below). I've determined that the most efficient way to cover this gap is simply to cover it with a triangle, and since I'm working in OpenGL, all I need are the points of the two points that don't overlap with the other rectangle, the third point being the center point where the two line segments(rectangles) are connected. How can I determine which points I need to use for the triangle, given that I have all of the points from both rectangles?
EDIT: I will also accept alternative solutions, as long as they cover up that gap.
EDIT 2: Nevermind, I solved it. I'll post code once I have better Internet connection.
Maybe I'm misunderstanding the question... but if you zoom in on the top corner of your red pentagon, you get something like this, am I right?
where A and B are nodes on the rectangle for edge1 and C and D are nodes on the rectangle for edge2. You say you already know these coordinates. And from what you say, the edges meet at the centre, which is halfway between A and B, and also halfway between C and D. So call this point X, and you can calculate its coordinates easily I guess.
So all you need to do is draw the missing triangle AXC, right? So one way would be to determine that A and C are on the "outside" of the polygon (and therefore need filling) and B and D are on the "inside" and therefore don't. But it's probably easier to just draw both, as it doesn't hurt. So if you fill AXC and BXD, you'd get this:
The solution I found assumes that there are 3 basic cases:
First, the three unique center points for the two rectangle proceed upward (positive y direction) so the gap is either on the left or right of the connection. In my code, I had the corner points of the rectangle organized by their orientation to the left or right of the center point, so if the bottom rectangle's left point is below the top rectangle's left point, then the gap is between the left points of the two rectangles, otherwise the gap is between the right points.
Second, the three unique center points have a maximum at the center most of the center points, so the gap is on the top. The gap is then between the two points with the maximum y values.
Third, the three unique center points have a minimum at the center most of the center points, so the gap is on the bottom. The gap is then between the two points with the minimum y values.
[I'll post pictures of the example cases if it is requested]

Finding a point clicked in a grid

Given this grid ( http://i.stack.imgur.com/Nz39I.jpg is a trapezium/trapezoid, not a square), how do you find the point clicked by the user? I.e. When the user clicks a point in the grid, it should return the coordinates like A1 or D5.
I am trying to write pseudo code for this and I am stuck. Can anyone help me? Thanks!
EDIT: I am still stuck... Does anyone know of any way to find the height of the grid?
If it is a true perspective projection, you can run the click-point through the inverse projection to find it's X,Z coordinates in the 3D world. That grid has regular spacing and you can use simple math to get the A1,D5,etc.
If it's just something you drew, then you'll have to compare the Y coordinates to the positions of the horizontal lines to figure out which row. Then you'll need to check its position (left/right) relative to the angled lines to get the column - for that, you'll need either coordinates of the end-points, or equations for the lines.
Yet another option is to store an identical image where each "square" is flood-filled with a different color. You then check the color of the pixel where the user clicked but in this alternate image. This method assumes that it's a fixed image and is the least flexible.
If you have the coordinates of end points of the grid lines then
Try using the inside-outside test for each grid line and find the position
Since this grid is just a 3D view of a 2D grid plane, there is a projective transform that transforms the coordinates on the grid into coordinates on the 2D plane. To find this transform, it is sufficient to mark 4 different points on the plane (say, the edges), assign them coordinates on the 2D plane and solve the resulting linear equation system.

VBA Bestfit position for datalabels on line chart

I was trying to make my own function to reposition data labels on line chart so they'll be in their best fit.
What does it mean best fit?
The data label wont be above the line itself (I took the higher angle of the point and put the data label in higher-angle/2 - so it will be in the middle of the higher angle) - I succeed to get the higher-angle but didn't succeed to get the position on graph (in pixels, relatively)
After u put the data label in the middle of the higher angle, you need to check with the other seriescollections, that the data label is not above one of other seriescollections line... - didn't reach this part.
and still I am not sure that's it...
Is anybody got something prepared? or someone wants to help me with it?
It is way over my level right now...
"Best fit" is a setting unique to pie chart data labels.
You have the option of positioning a line chart's data labels centered (directly on a point), as well as above, below, left of, and right of the point. You can also position the data label anywhere by changing the .left and .top properties of the label. Left and Top are in points with respect to the top left corner of the chart, so you will have to brush up on your algebra to fine tune label positioning.
As with all else involving charts, Keep It Simple is sound advice. Do you need to label everything, or can you get away with a few key labels?
So after some tries, I decided to ditch the idea since this's way too complicated for me.
What I can say is that I ditch it after I stepped up a little bit.
I could find the best position for each label only when there's one line.
I achieved it by calculating the bigger arc (assumed each point is the middle of a circle), and than put it in the middle of that arc.
to do it when you have more than one line, you have to consider the position of the points of the other line/s. Therefore it becomes much more complicated and this is the reason I decided to ditch it.

Resources