I'd like to find the relative position of the graph origin in order to display the correct mouse position when it's inside it. But the size and pos property of the graph class are relative to the labels too.
How can I find the absolute position of the point 0,0 on the graph?
There is a member Graph._plot_area. It is a StencilView and it's position and size are equal to the plot area of the graph, meaning it ranges from the start of the xmin, ymin to xmax, ymax in pixel position.
If graph point x,y = 0,0 is in view, it should be equal to _plot_area.pos, however if 0,0 is not in view, you will have to calculate where it is expected to be using a ratio for the x and y axis, they can be calculated by:
x_ratio = (xmax - xmin) / _plot_area.width
y_ratio = (ymax - ymin) / _plot_area.height
and the point calculated by:
x = xmin + x_ratio * ( x - xmin)
y = ymin + y_ratio * ( y - ymin)
Hope this helps!
Related
I don't know if this is basic math I have to compute, or my inexperience with the pitch, roll, and yaw values. At the moment I have an image object that moves based on my accelerometer values.
//Move the ball based on accelerator values
delta.x = CGFloat(acceleration.x * 10)
delta.y = CGFloat(acceleration.y * 10)
ball.center = CGPointMake(ball.center.x + delta.x, ball.center.y + delta.y)
I can calculate the pitch through the attitude and get the angle. What I want to do is line up my "ball" in the center of the screen only when the angle of the phone is a certain angle, lets say 45 degrees. How can I move my ball so that it lines up in the center based on specific angles given?
Your screen height is Η pixels.
Your screen width is W pixels.
The horizontal centre of the screen is x = W / 2
I'm assuming from your question you want the ball centre to vary between the top (x, 0) when the screen is flat and bottom (x, H) when the screen is vertical.
If the angle of your phone θ varies between 0 and π, then y = θ / π * H
ball.center = CGPoint(x: W / 2, y: θ / π * H)
All you need is the trig to work out θ based on the gyro readings
Question: How can one position a polygon relative to one of it's known vertice points?
In other words how could I calculate where the auto generated center of the polygon is relative to one of the known vertices (i.e. used in the path)?
e.g. Image placing a specific shape on a map which you make polygon, you then want to position it on the map, however you can't do this accurately without knowing where it's Corona engine created centre is. Extract from API: "The local origin is at the center of the polygon and the anchor point is initialized to this local origin."
PS Actually wondering if I should be using a line and appending points to create effectively a polygon, however perhaps you can't add background color in this case(?)
The center calculated by corona is the center of the bounding box of the polygon.
I assume you have a table with all the points of your polygon stored like that:
local polygon = {x1,y1,x2,y2,...,xn,yn}
1) to find the bounding box of your original points, loop thru all the points; the smallest x and smallest y values will give you the coordinates of the top-left point; the largest x and y values are for the bottom-right point;
local minX = -math.huge
local minY = -math.huge
local maxX = math.huge
local maxY = math.huge
for i=1, #polygon, 2 do
local px = polygon[i]
local py = polygon[i+1]
if px > maxX then maxX = px end
if py > maxY then maxY = py end
if px < minX then minX = py end
if py < minY then minY = py end
end
2) find the center of this bounding box:
local centerX = (maxX - minX)/2
local centerY = (maxY - minY)/2
3) add the center point to the top-left point
local offsetX = centerX + minX
local offsetY = centerY + minY
4) add this offset to the corona polygon to place it in the same position as the original polygon.
Should work bot I have not tested it. Let me know.
I used a variant on the solution above as I couldn't get it to work. Essentially I found the minimum vertex coordinates in each dimension and added them to the polygon position. By comparing them to the contentBounds positions, I can compute the difference between where I thought the minimums would be and where they are.
local min_x = math.huge
local min_y = math.huge
for v = 1, #vertices, 2 do
min_x = math.min(min_x, vertices[v])
min_y = math.min(min_y, vertices[v + 1])
end
local poly = display.newPolygon(x, y, vertices)
local offset_x = (x + min_x) - poly.contentBounds.xMin
local offset_y = (x + min_y) - poly.contentBounds.yMin
poly:translate(offset_x, offset_y)
I want to detect rectangles and its corners via harris corner detector. It contains a block with corners:
filter.cornersDetectedBlock = { (cornerArray:UnsafeMutablePointer<GLfloat>, cornersDetected:UInt, frameTime:CMTime) in
The problem is, cornerArray is of type GLfloat and it returns a value between 0 and 1. I don't know how to create something like CGPoint with x and y values. Any ideas how to achieve this?
Thanks!
I don't know the specifics, but in general you have to interpolate.
I assume that you're getting back values for x and y that both range from 0 to 1, where 0 is the left/bottom edge, and 1 is the right/top edge?
You just need to lay out a ratio and convert from one coordinates system to the other.
0...1000
is to
0...1
.5 x
---- = ----
1 1000
x * 1 = 0.5 * 1000
x = 0.5 * 1000 / 1
x = 500
So if you get a value 0.5 it would be halfway between 0 and 1000. (1000-0) * 0.5. If your pixel rectangle has an origin of 0,0, you'd just multiply your 0..1 x value by your width and your 0..1 y value by your pixel height. If the pixel origin is not 0 then you need to add the origin.
How do I find the maximum rounding I can apply to either corner for any amount of rounding on the other corner?
Answers to questions from the comments:
1) The inner and outer large arcs (those that are 90 degrees wide here) always have the same center
2) When asking for the maximum rounding that you can do, what are the constraints on the other, smaller circle? Does it need to be at least some radius? Otherwise you are doing to end up with just one rounding.
One of the two rounding circle's radius is given. There are no other constraints other than the maximum of the other circle which I just can't find.
If the "fixed" corner that I refer to has zero rounding then I'm searching for the maximum rouding that can be applied with only the other corner.
3) What constitutes as the maximum rounding? Are you trying to choose between the two examples above? Or is finding either of those cases considered a solution?
Either of the shown cases is a perfect solution. E.g. in the first image the the radius of the smaller circle might be given. Then I'm looking for the maximum radius of the larger one.
These images are just examples for perfect solutions.
4) is there any constraints on the two arcs? What happens if the arcs can't fit a full circle? Would the answer be the largest that fits?
How exactly do you mean that the arcs can't fit a full circle?
The all circles are perfect circles, but I can't figure out the max size of the rounding possible, or how to calculate it's position. Here's some images that describe the problem.
Given that the origin of the coordinate system is at the center point of the inner and outer large arcs...
For the first case where the large circle is tangent to the outer edge, the center point of the large circle is
x = R cos(t) / (1 + cos(t))
y = R sin(t) / (1 + cos(t))
where R is the radius of the outer arc segment, and t is the angle between the x-axis and the ray from the origin through the center of the large circle.
For the second case where the large circle is tangent to the inner edge, the center point of the large circle is
x = R cos(t) / (1 - cos(t))
y = R sin(t) / (1 - cos(t))
where R is the radius of the inner arc segment, and t is the angle...
In both cases, the radius of the circle is equal to its x coordinate. The range of t is between some minimum angle and PI/2. At PI/2, the circle is vanishingly small. At the minimum angle, the y value is equal to the opposite radius. In other words, for the first case where the large circle is tangent to the outer edge, the minimum angle is such that y is equal to the inner radius. Whereas if the circle is tangent to the inner edge, the minimum angle is such that y is equal to the outer radius. It can be proven mathematically that the minimum angle is the same for both cases (tangent to inner and tangent to outer both have the same minimum angle for a given inner and outer radius). However, computing the minimum angle is a bit of a challenge. The only way I know how to do it is by playing the high/low game, e.g.
- (CGFloat)computeAngleForOuterTangentGivenY:(CGFloat)Y
{
CGFloat y;
double high = M_PI_2;
double low = 0;
double mid = M_PI_4;
while ( high - low > 1e-9 )
{
y = (self.outerRadius * sin( mid )) / (1.0 + cos( mid ));
if ( y > Y )
high = mid;
else
low = mid;
mid = (low + high) / 2.0;
}
return( mid );
}
- (CGFloat)computeAngleForInnerTangentGivenY:(CGFloat)Y
{
CGFloat y;
double high = M_PI_2;
double low = 0;
double mid = M_PI_4;
while ( high - low > 1e-9 )
{
y = (self.innerRadius * sin( mid )) / (1.0 - cos( mid ));
if ( y > Y )
low = mid;
else
high = mid;
mid = (low + high) / 2.0;
}
return( mid );
}
It takes about 30 passes for the loop to converge to an answer.
To find the coordinates of the small circle, note that the small circle has the same y value as the large circle, and is tangent to the opposite edge of the arc segment. Therefore, compute the angle t for the small circle based on its y value using the appropriate high/low algorithm, and then compute the x value using the formulas above.
QED
The question isn't posed correctly without showing both ends of the line segment. Suppose for a moment that each line segment is a data structure that maintains not only the end points, but also cap radius in each point, and also knows the angle going out to the next endpoint that this line will attach to. Each cap radius will subtract from the length of the line segment that has to be stroked as a rectangle. Assume you have a line of interest between points B and C, where B joins to another (longer) segment A, and C joins to another (longer) segment D. If line BC is length 10, with cap radius B and cap radius C both set to 4, then you will only render rectangle of length 2 for the straight part of the line segment, while length 4 is used to draw the arc to A, and another length 4 is used to draw the arc to D.
Furthermore, the maximum cap radius for C is constrained not only by BC and B's cap radius, but also by CD and D's cap radius.
Suppose I have a map, for example from openstreetmaps.org.
I know the WGS-84 lat/lon of the upper left and lower right corner of the map.
How can I find other positions on the map from given WGS-84 lat/lon coordinates?
If the map is roughly street/city level, uses a mercator projection (as openstreetmap.org seems to), and isn't too close to the poles, linear interpolation may be accurate enough. Assuming the following:
TL = lat/lon of top left corner
BR = lat/lon of bottom right corner
P = lat/lon of the point you want to locate on the map
(w,h) = width and height of the map you have (pixels?)
the origin of the map image, (0,0), is at its top-left corner
, we could interpolate the (x,y) position corresponding to P as:
x = w * (P.lon - TL.lon) / (BR.lon - TL.lon)
y = h * (P.lat - TL.lat) / (BR.lat - TL.lat)
Common gotcha's:
The lat/lon notation convention lists the latitude first and the longitude second, i.e. "vertical" before "horizontal". This is opposite to the common x,y notation of image coordinates.
Latitude values increase when going in a north-ward direction ("up"), whereas y coordinates in your map image may be increasing when doing down.
If the map covers a larger area, linear interpolation will not be as accurate for latitudes. For a map that spans one degree of latitude and is in the earth's habitable zones (e.g. the bay area), the center latitude will be off by 0.2% or so, which is likely to by less than a pixel (depending on size)
If that's precise enough for your needs, you can stop here!
The more precise math for getting from P's latitude to a pixel y position would start with the mercator math. We know that for a latitude P.lat, the Y position on a projection starting at the equator would be as follows (I'll use a capital Y as unlike the y value we're looking for, Y starts at the equator and increases towards the north):
Y = k * ln((1 + sin(P.lat)) / (1 - sin(P.lat)))
The constant k depends on the vertical scaling of the map, which we may not know. Luckily, it can be deduced observing that y(TL) - y(BR) = h. That gets us:
k = h / (ln((1 + sin(TL.lat)) / (1 - sin(TL.lat))) - ln((1 + sin(BR.lat)) / (1 - sin(BR.lat))))
(yikes! that's four levels of brackets!) With k known, we now have the formula to find out the Y position of any latitude. We just need to correct for: (1) our y value starts at TL.lat, not the equator, and (2) y grows towards the south, rather than to the north. This gets us:
Y(TL.lat) = k * ln((1 + sin(TL.lat)) / (1 - sin(TL.lat)))
Y(P.lat) = k * ln((1 + sin(P.lat )) / (1 - sin(P.lat )))
y(P.lat) = -(Y(P.lat) - Y(TL.lat))
So this gets you:
x = w * (P.lon - TL.lon) / (BR.lon - TL.lon) // like before
y = -(Y(P.lat) - Y(TL.lat)) // where Y(anything) depends just on h, TL.lat and BR.lat