Camera API- How to focus on center of a polygon in images? - java

Whats the issue?
I am taking pictures with resolution, let's say 4096x3072. Now, I have a polygon inside this picture with some co-ordinates {(x1,y1),(x2,y2),(x3,y3),(x4,y4)}. I want the focus of my camera in the center of this polygon.
Anything on internet?
I have looked over internet but most of them are for finger touch event. They are getting co-ordinates like motion.eventX() and motion.eventY() but these co-ordinates map to screen resolution not the picture resolution.

I am not 100% sure but i think this will work for you: so you have an n number of points that define the polygon. Add all the x and divide them by the number of points: X=(x1+x2+...+xn)/n. Same with the y: Y=(y1+...+yn)/n. The center of the polygon should be (X,Y). I just came up with it and it works for the example i tested it on. If it dosent work on a case and i missthoguht it tell me.
EDIT: red the question wrong, be right back with edit with the actual answer :D

Related

Get coordinates of displayed rectangle

I'm trying to create a game for Android device and I have a small question about the rendering of the scene. Effectively I want to draw a square of a precise size but I'm not pretty sure about the way I can get the coordinates of the border of the screen in openGL dimension. My application is set in landscape mode, so computation looks easier.
I have drawn a square with a border size of 2 and I have the impression that the square takes all the height of the screen. Since I know the resolution of my screen which is equal to 1920*1080, I can compute the width of my scene. Then, by drawing several squares I found the coordinates of on corner.
This way of computing the coordinates are a bit weird and I'm not pretty sure that the computation will always lead to a good answer. Is there a nicer way and obviously a better way to compute those coordinates ?
Thank you in advance !

Shape Rotation Issue - Java2d

I am developing one simple game in which i have encountered one small but important issue.
I have implemented absolute rotation in my logic.
When i start rotating an object when the object does not have any rotation , it works fine and i can rotate as in any direction without any problem as shown in the following link.
Initial Rotation Video
Now the problem arise when the object does have some rotation , and why i try to rotate in one of the direction , instead of being rotated in desire direction the rotation always starts from initial rotation as shown in the following link.
Rotation issue when shape has some rotation
I think the video shows everything , still if you have any questions please ask me.
I think the problem is , there should be a relative rotation in the direction of mouse pointer from whatever circle is selected .
Now about My Logic,
in mouse press event i just checked
Mouse Press
Whether the shape is selected on the canvas , if yes
if one of the four circles contains mouse point if yes
then initiateRotation
Mouse Drag
Using Vector Maths
I update the motion according to mouse points ,
calculate rotation angle according to the following method
Math.atan2(rotationVector.getY(), rotationVector.getX());
and apply rotation on this shape.
Rotation Vector i get from this class
Vector Rotation
I called above class startMotion in mouse press and updateMotion in mouse drag event.
What am i missing or doing anything wrong ?
We need to see some code to be able to help you out. It looks like you reset the rotation, whenever you initiateRotation, and then the object quickly rotates in place, according to your mouse position when you drag.

Android inscribed circle

I am currently working on Code based drawables in Android but ran into a little bump, I have a rectangular LinearLayout that I want to set it's drawable to a circle I have reasons for doing this in code so please do not recommend xml, I did some searching on Google and was only really able to come up with the opposite an inscribed rectangle in circle and that is not what I need.
So let's say I have a rectangle of 80 wide and 120 in length(these are random guesses, and need to be able to change) what would be the formula I need to use to determine the largest circle I can create in this rectangle and when I say circle let me be more specific I do not want a oval I want a full circle.
So this question is simply what would be the proper formula needed for this. Thank you for your time =)
int Radius = Math.min( width, height ) / 2;
That is the largest radius circle that can fit in the rectangle
Well I'm back again and feel slightly stupid on this one,
The answer came to me during good old shower time (doesn't it always). I was thinking about my problem in the 3d senses something I regretfully did not do before I posted on here. It occurred to me that if I want to make a for lack of better term a perfect circle that the diameter of this circle would always be constricted by the smaller side of the rectangle and then you can account for that to center the circle.
So in my scenario my circle with be 80 in diameter because it would be restricted by the width of the rectangle. Sorry for wasting your time have a good day =)

Rotating an image back to it's original state

I have a program that needs to take in a photo taken by an iphone (or any kind of decent camera) of a 7x10 grid with a thick black boarder around the edges. This image can be received rotated to the right or to the left (there's no need to worry about skew). I have an image of the grid in its original state already, but I need to get the picture that I'm taking in and rotate it to its "perfect/original" state.
Idea 1: Performance Hog/Bad Results
Threshold the picture that I receive and the perfect grid Image I already have. Compare each pixel for 0 rotation, get a total score, and save it. Do do this rotating the image of increments by 1 to 359. The lowest score is the rotation we need to get the picture back to its original state.
Idea 2: Still Unsure How To Go About Doing This
Threshold the picture that I receive and the perfect grid Image I already have. Draw a a line through the center of the picture vertically and horizontally. Find the rotation based on the black pixel count that the vertical and horizontal line passed through. This would require some sort of Trigonometry that I'm not to great with understanding.
Does anyone have any other ideas for getting this working?
Any help for pointing me in the right direction would be greatly appreciated!
Thanks!
Instead of drawing one horizontal and one vertical line, draw instead two horizontal lines (say, each at a third of the picture). Only look at the left halves of these lines and calculate how many black pixels there are on the path of each (a1 and a2). You also have to keep track of the distance between the two red lines, so the number of pixels d.
Using this notation in the figure above, your desired angle is:
alpha=atan2((a2-a1),d)
and a counterclockwise rotation by alpha will bring the white portion of the picture into proper alignment.

Ray tracing - constructing ray through pixel

I have an assignment to implement ray tracing in Java.
I'm not asking for much, just to have some information on how to construct the rays from the camera through a pixel given its x and y. I've found over the Internet a lot of sources that explain that but in 2D, and I need how to do that in 3D.
Thanks in advance
The question is how to find the coordinates in space of a point on the screen whose position is given by (x,y) in screen coordinates.
I don't know what coordinates system you're using for the screen, so I'll make some educated guesses and you can adjust accordingly.
The center of the screen has known location [X,Y,Z]center in space. I'll presume the origin of the screen coordinate system is there. We have a "direction" vector d which is normal (perpendicular) to the screen, and an "up" vector u. I'll presume that the +y direction on the screen is u. We can take the cross-product of these vectors, r = dxu, which I will take to be the +x direction on the screen. So the location of a point on the screen whose screen coordinates are (x, y) will be [X, Y, Z]center + xr + yu, and we're done.
The basic idea is this: you have a camera at a given position (x,y,z) with a given resolution. You have a set of light sources. You have an orientation of the camera (an angle, think of how you'd tilt/rotate your head to look up/down etc...). Now what you want to do is essentially for each camera pixel, "extend" perpendiculars (rays) until they touch geometry. Then you know "what to render" (namely the bit of geometry you've just found). Next up is to determine whether or not the object is shadowed, which you do by "extending" rays towards the light sources until they either touch geometry (your spot is shadowed by the other bit of geometry, for the given the light source) or until they reach the light source (your spot is lit by the given light source).
That's the basics, it gets a lot more difficult when you consider things like reflection, diffusion of light, and so on.

Categories