I need help transforming a selected point in the Rotated View back to it's corresponding point in the original Image. So for example if I clicked in the upper left ( 0,0 ) in the rotated view, it should correspond to (0,1280) in the original image.
Extra points for a solution that works regardless of the rotation.
Original Image ( 1920 x 1280 ) Rotated View ( for display on phone )
+----------------------------+ +-----------------+
|(0,0) | |(0,0) | ( 1280 x 1920 )
| | | |
| | | x |
| x | | ( click ) |
| ( what is this point ) | | |
| | | |
| | | |
+----------------------------+ | |
(1920,1280) | |
| |
| |
| |
| |
| |
| |
| |
+-----------------+
(1280,1920)
UPDATED
/*
This is how I build the matrix used to perform the initial rotation from the original to the rotated image. This matrix also includes scaling
Code base: Android/Java
bitmap ( bitmap i'm scaling/rotating )
canvas ( the canvas being drawn to )
Note: bitmap is in landscape mode / canvas is in portrait
*/
Matrix matrix = new Matrix();
float centerX = canvas.getWidth() >> 1;
float centerY = canvas.getHeight() >> 1;
rAngle = 90;
scaleH = ((float) canvas.getHeight()) / bitmap.getWidth();
scaleW = ((float) canvas.getWidth()) / bitmap.getHeight();
scaler.preScale(scaleH, scaleW);
scaler.postRotate(rAngle, centerY, centerX);
float nx = (canvas.getHeight() - canvas.getWidth()) / 2;
scaler.postTranslate(-nx, nx);
canvas.drawBitmap(bitmap,scaler,null);
I'm hardly a math guy, so any hand holding will be appreciated. :)
Subscript O indicates coordinates in the original frame and subscript R in the rotated frame:
XO = YR
YO = maxXR - XR
The four corners of the frame give us:
For top-left in the rotated frame (0,0)
XO = 0
YO = 1279 - 0 = 1279
(0, 1279)
For top-right, (1279, 0):
XO = 0
YO = 1279 - 1279 = 0
(0, 0)
For bottom-left, (0, 1919):
XO = 1919
YO = 1279 - 0 = 1279
(1919, 1279)
For bottom-right, (1279, 1919):
XO = 1919
YO = 1279 - 1279 = 0
(1919, 0)
Related
i want to code a video platform and have a problem and can't think of a solution right now.
I want to divide a rectangle into equal parts
Came up with a solution for a square but i am unable to come up with a solution for different ratios.
Maybe u guys can help me.
example:
BAD GOOD
n=4
________ ________
| | | | | | | |
| | | | | |---|---|
|_|_|_|_| |___|___|
n=2
________ ________
| | | | |
| | | | |
| | | |–––––––|
| | | | |
|___|___| |_______|
Let X be the width of the rectangle and let Y be the height. Let the goal be to divide this rectangle into N rectangles of equal area whose sides are as close to equal as possible.
The solution is not difficult. First, find all factors of the N. Then, write N as a product of two numbers A and B such that A and B are as close as possible (that is, there is no other choice A' and B' such that |A' - B'| < |A - B|). Assume we chose A > B. All we have to do is put A - 1 lines along the long side of the rectangle, and B - 1 lines along the short side.
For example: n = 4, A = 2 and B = 2 is optimal, so you put A - 1 = 1 and B - 1 = 1 lines along each side of the rectangle (as in your GOOD column for n = 4).
For example: n = 21, A = 7 and B = 3 is optional, so you'd put 6 lines along the long edge of the rectangle and 2 lines along the short edge, equally spaced:
_ _ _ _ _ _ _
|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|
Of course, for prime values of N, you are not going to get a very nice solution, but then there is no nice solution in that case. In such cases - where A and B are very very different and the rectangle's dimensions are not similarly different - you might want to choose another solution that doesn't require all rectangles have the same side lengths. You could do better by allowing 2 or 3 kinds of rectangles, or more, into the solution, for instance.
I found this algorithm to check if two polygons intersect:
https://rbrundritt.wordpress.com/2008/10/20/determine-if-two-polygons-overlap/#comment-6287
//poly1 and poly2 are arrays of VELatlongs that represent polygons
function ArePolygonsOverlapped(poly1, poly2)
{
if(poly1.length >= 3 && poly2.length >= 3)
{
//close polygons
poly1.push(poly1[0]);
poly2.push(poly2[0]);
for(var i = 0; i < poly1.length-1;i++)
{
for(var k = 0; k < poly2.length-1; k++)
{
if(SimplePolylineIntersection(poly1[i],poly1[i+1],poly2[k],poly2[k+1])!=null)
return true;
}
}
return false;
}
return null;
}
function SimplePolylineIntersection(latlong1,latlong2,latlong3,latlong4)
{
//Line segment 1 (p1, p2)
var A1 = latlong2.Latitude - latlong1.Latitude;
var B1 = latlong1.Longitude - latlong2.Longitude;
var C1 = A1*latlong1.Longitude + B1*latlong1.Latitude;
//Line segment 2 (p3, p4)
var A2 = latlong4.Latitude - latlong3.Latitude;
var B2 = latlong3.Longitude - latlong4.Longitude;
var C2 = A2*latlong3.Longitude + B2*latlong3.Latitude;
var determinate = A1*B2 - A2*B1;
var intersection;
if(determinate != 0)
{
var x = (B2*C1 - B1*C2)/determinate;
var y = (A1*C2 - A2*C1)/determinate;
var intersect = new VELatLong(y,x);
if(inBoundedBox(latlong1, latlong2, intersect) &&
inBoundedBox(latlong3, latlong4, intersect))
intersection = intersect;
else
intersection = null;
}
else //lines are parrallel
intersection = null;
return intersection;
}
//latlong1 and latlong2 represent two coordinates that make up the bounded box
//latlong3 is a point that we are checking to see is inside the box
function inBoundedBox(latlong1, latlong2, latlong3)
{
var betweenLats;
var betweenLons;
if(latlong1.Latitude < latlong2.Latitude)
betweenLats = (latlong1.Latitude <= latlong3.Latitude &&
latlong2.Latitude >= latlong3.Latitude);
else
betweenLats = (latlong1.Latitude >= latlong3.Latitude &&
latlong2.Latitude <= latlong3.Latitude);
if(latlong1.Longitude < latlong2.Longitude)
betweenLons = (latlong1.Longitude <= latlong3.Longitude &&
latlong2.Longitude >= latlong3.Longitude);
else
betweenLons = (latlong1.Longitude >= latlong3.Longitude &&
latlong2.Longitude <= latlong3.Longitude);
return (betweenLats && betweenLons);
}
However, when I was testing this algorithm with these two polygons, Checking on google maps polygon 1 contains polygon 2. But the algorithm is returning false, i.e. these polygons do not overlap. Please suggest how to fix this.
Polygon 1:
————–+——————+——————+
| polygon_id | latitude | longitude |
+————–+——————+——————+
| 158 | 13.1303042583903 | 77.7543640136719 |
| 158 | 13.1420061213258 | 77.6383209228516 |
| 158 | 13.1189362005413 | 77.5209045410156 |
| 158 | 12.9209143604345 | 77.3890686035156 |
| 158 | 12.7970707734707 | 77.4900054931641 |
| 158 | 12.8446071570180 | 77.6403808593750 |
| 158 | 12.8499628062145 | 77.8154754638672 |
| 158 | 12.9436681426755 | 77.8504943847656 |
| 158 | 13.0420208479226 | 77.8154754638672
Polygon 2:
+--------------+------------------+------------------+
| polygon_id | latitude | longitude |
+--------------+------------------+------------------+
| 150 | 12.8969871916682 | 77.5722312927246 |
| 150 | 12.9225875033164 | 77.6108551025391 |
| 150 | 12.8817596199286 | 77.6197814941406 |
| 150 | 12.8887878450392 | 77.5693130493164 |
+--------------+------------------+------------------+
The part about the intersection looks correct to me, but I don't think it is possible to assert that a polygon is included into another polygon so easily. In fact, if we look more carefully at the code, the algorithm is able to say if polygon A is included into polygon B by only looking at a segment of A and a segment of B. It means that, without knowing anything else about the polygons, giving one segment of each polygon is enough to know if these polygons are included one into the other. That is clearly wrong.
How can I find the symbolic inverse of a matrix in R; for example:
Matrix.test <- function(x) matrix(c(x, x^2, x^3, x^4, x^5, x^6, x^7, x^8, x^9, 2*x, 3*x, 4*x, 2*x^2, 3*x^3, 4*x^4, 5*x^5), 4, 4)
I Know there is a package called 'Ryacas' which is an interface to 'yacas', but I could not apply it to do such this computations.
'yacas' is a program for symbolic manipulation of mathematical expressions.
Please see link for more details.
thank you
It works fine for me:
> library(Ryacas)
> x <- Sym('x')
> M <- List(List(1,x),List(x,1))
> PrettyForm(M)
/ \
| ( 1 ) ( x ) |
| |
| ( x ) ( 1 ) |
\ /
> PrettyForm(Inverse(M))
/ \
| / 1 \ / -( x ) \ |
| | ------ | | ------ | |
| | 2 | | 2 | |
| \ 1 - x / \ 1 - x / |
| |
| / -( x ) \ / 1 \ |
| | ------ | | ------ | |
| | 2 | | 2 | |
| \ 1 - x / \ 1 - x / |
\ /
And following on:
M2 <- List(List( x, x^2, x^3, x^4),
List( x^5, x^6, x^7, x^8),
List( x^9, 2*x ,3*x , 4*x),
List(2*x^2, 3*x^3, 4*x^4, 5*x^5))
Inverse(M2)
The answer's pretty complicated, though (I could only be bothered to re-format the first four lines):
{{(x^6*3*x*5*x^5-x^6*4*x*4*x^4+x^8*2*x*4*x^4-x^7*2*x*5*x^5+
x^7*4*x*3*x^3-x^8*3*x*3*x^3)/(x^7*3*x*5*x^5-x^7*4*x*4*x^4-
x^8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x*2*x^2)}};
Given a grid where I know the number of rows (which is fixed), and I know the current count of columns (which can grow arbitrarily), how do I calculate the row and column of an square from it's index?
+ + + + +
Cols ---> | 0 | 1 | 2 | 3 | ...
+--+---|---|---|---|---
0 | 0 | 3 | 6 | 9 | ...
+--+---|---|---|---|---
Rows 1 | 1 | 4 | 7 | A | ...
+--+---|---|---|---|---
2 | 2 | 5 | 8 | B | ...
+--+---|---|---|---|---
. . . . . ...
. . . . . .
. . . . . .
So, given:
final int mRowCount = /* something */;
int mColCount;
And given some function:
private void func(int index) {
int row = index % mRowCount;
int col = ???
How do I correctly calculate col? It must be a function of both the number of columns and rows, I'm pretty sure. But my brain is failing me.
Sample: If index == 4, then row = 1, col = 1. If index == 2 then row = 2, col = 0.
Thanks.
int col = index / mRowCount;
Didn't really understand your setup but if you got a normal grid with a progressive index like in the Android GridLayout:
+-------------------+
| 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|
| 10| 11| 12| 13| 14|
|---|---|---|---|---|
| 15| 16| 17| 18| 19|
+-------------------+
The calculation is:
int col = index % colCount;
int row = index / colCount;
For example:
row of index 6 = 6 / 5 = 1
column of index 12 = 12 % 5 = 2
index = col * mRowCount + row
then
row = index % mRowCount;
col = index / mRowCount;
I believe the column would be obtained by integer division:
int col = index / mRowCount;
It would be possible to limit it to a single division (eliminate the modulus operation) by replacing it with a multiplication and subtraction. I'm not sure if that is less costly; probably wouldn't matter in most situations:
int col = index / mRowCount;
int row = index - col * mRowCount;
row = CEILING(index / numberOfColumns)
CEILING rounds up to the next integer
col = MOD(index / numberOfColumns)
and this with one exception which you must take into account --> when MOD=0, when col result is ZERO, then you set your col=numberOfColumns
(so for example, let say numberOfColumns=48... then, MOD(96, 48) is ZERO, and so is MOD(48, 48)=0 ... cause anything divisible by 48 will be MOD=0... in other words, when MOD=0 you are in the LAST or HIGHEST column, you are in numberOfColumns column
column = index / max_rows;
row = index % max_rows;
row = index / numberOfColumns
and
column = index % numberOfColumns
I have a dataset with three variables. One continous independent variable, one continous dependent variable, and a binary variable that catagorizes how the measurements were taken. Using ggplot, I know that I can make a scatter plot with the points colored by the catagory:
g <- ggplot(dataset, aes(independent, dependent))
g + geom_point(aes(color=catagory))
However, I want to know if there is a way to make a graph where there is a vertical line comming up from points of catagory 0 and a vertical line going down from points of catagory 1. It would look something like this:
- | | |
| | | |
| | | |
| | | |
- | | o |
| | | | |
| | o | | |
| | o | | | |
- | | | o | o
| | | | |
| o | | |
| | | |
+----|-----|-----|-----|-----|
The reason for wanting a plot like this is that one category represents an upper bound (the points with lines going downwards) and one represents a lower bound (the points with lines going upwards). Having these lines would make it easy to visualize the area which is between these bounds, and whether a function plotted on top could accurately represent the data:
- | | |
| | | |
| | | |
| | | |
- | | o | _____
| | | |_|__/
| | o |_/| |
| | o |__/| | |
- | | /| o | o
| _|_|/ | |
| / o | | |
|/ | | |
+----|-----|-----|-----|-----|
If there is any way to do this using ggplot or any other graphing library for R, I would love to know how. However, if it isn't possible, I'd be open to hearing other ways to represent this data. Simply distinguishing the catagories based on color doesn't do enough to emphasize the upper/lower bound nature of the catagories for my purposes.
The following could work for you, I hope I understood the problem well.
First, generating some random data for the dataframe, as no sample data was provided. The random numbers will make the plot ugly, I hope it will look better with real data:
dataset <- data.frame (
independent = runif(100),
dependent = runif(100),
catagory = floor(runif(100)*2))
Next, find the upper or lower part of the plot (=min/max of values) based on "catagory" for every case:
dataset$end[which(dataset$catagory == 0)] <- max(dataset$dependent)
dataset$end[which(dataset$catagory == 1)] <- min(dataset$dependent)
Now, we can plot data with geom_segment().
g <- ggplot(dataset, aes(independent, dependent, min, max))
g + geom_segment(aes(x=independent, y=dependent, xend=independent, yend=end, color=catagory))
Note, that I also added + theme_bw() + opts(legend.position = "none") parameters to the plot as it looked very strange with random datas.