Using the OpenCV for Processing library by Greg Borenstein, I am able to detect a user's face within a webcam stream, and draw a rectangle around it. Here is the portion that draws the rectangle to the screen:
Rectangle[] faces = opencv.detect()
for (int i = 0; i < faces.length; i++)
{
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
}
As the user moves their face, the rectangle will move accordingly.
I want to draw a line on the screen where one point is set at predefined location and the other is always pinned to the bottom right hand vertex of the rectangle. This way the length and angle of the line will change with respect to the rectangle's location.
One thing I tried was to subtract some values from faces[i].x and faces[i].y until I reached the bottom right vertex, but I found that the depth of the face in the webcam made this method not work.
With that, how could I find the above mentioned vertex of the rectangle so that I can properly draw the line?
The x position of a rectangle gives you its left side. So x+width gives you its right side.
Similarly, the y position of a rectangle gives you its top side. So y+height gives you its bottom side.
float rectRight = faces[i].x + faces[i].width;
float rectBottom = faces[i].y + faces[i].height;
If that's still not what you're looking for, then please post a small example that we can actually run. Use hardcoded values instead of relying on opencv, so we can copy and paste your code and see the problem ourselves.
Related
How to invert Y axis? When I touch on bottom or top of the screen, the Y value is opposite I want
You can't invert an axis per se. You see, in computer graphics, the 2D coordinate system is a bit different from the canonical one taught at school in maths. The difference is that in computer graphics the y-axis is in the opposite direction, that is, from the origin to the bottom it has positive values and from the origin to the top it has negative values. Also, the origin is at the top left corner of the screen. If you can't get used to it then you can always take the opposite value to what you get, for this, asume ycoord holds the value obtained then you can do ycoord = -ycoord and that will get you the value as you're used to. Also, if you want the origin to be in the bottom left corner then you should check your y-coordinate, if it's positive then substract the vertical resolution to it, and if it's negative then add the vertical resolution to it.
But keep mind that you're going against the standard definition for coordinate systems in computer graphics.
I would say this is a duplicate questions of this one:
Move a shape to place where my finger is touched
Check on my answer there, so I won't repeat my self.
Or in short - use camera.unproject() method to get world coordinates from screen coordinates.
I am working in an android application where i need to draw a graph like this.
I have drawn the arc using paint and canvas but i didn't know how to draw the line path along with the text as mentioned in below picture!
Any heads up on this would be really helpful for me. Thanks in advance.
In order to keep direction of the line truly, you have to use many trigonometric functions and calculus. However, for such cases you can use canvas.rotate() for tricky solution. For the solution, first you calculate angle of line according to a value. For example, assume your arc represents total value of 200. The left side is 0 and the right side is 200, then you get the value of 80. With these values, you can calculate the angle like that 180degree * (80f)/(200 - 0) it gives 72 degree. Then you can rotate the canvas for drawing canvas.rotate(70f,centerX,centerY). CenterX and CenterY are values of the center point of the arc. After that, you can draw your line as you draw to line at the left-bottom corner of the canvas canvas.drawLine(0,100,20,100,paint).
canvas.save()
canvas.rotate(70f,centerX,centerY)
canvas.drawLine(0,100,20,100,paint)
canvas.restore()
I'm building a prototype android app and I'm trying to make a circular layout. Basically, I have a centre point, and I want to be able to place other elements in a circle around it, like this. There's a library called ArcLayout that I tried using, and it works well, but it doesn't quite work for what I want to do. The elements I'm trying to place have a dynamic distance from the centre, and may have similar or identical distances at any given time.
I could just create a new arc layout for each distance level, but I'm trying to avoid something like this. Ideally, each element in the layout should position itself at a maximum distance from all other elements, while staying at the correct distance from the centre.
I've looked up a lot of different things from radar graphs, to orbiting animations, and none of them seem to work with what I want to do. How would I go about making something like this?
Here is a high level description of how I would do this:
Make your original view extend View class. In your ondraw method, do the following:
first draw the circle in the center of the view by getting the coordinate for the small circle by getWidth()/2, getHeight()/2. set some fixed radius. Store the center as cx,cy
Now, you need to draw other circles at the right position, for that you need the center of each circle. Now, from cx,cy if you have the angle it makes with x - axis and the distance from cx,cy to the new point, you can get the coordinates to the new points as follows:
cx1 = cx + r*cos(theta) and cy2 = cy + r*sin(theta).
where r is the distance between the points and theta is the angle between line joining the points and the positive x-axis.
Just remember to convert degrees to radians in your calculations.
Once you get coordinates for the new circle, just draw it using canvas.drawCircle method.
Repeat this method as often as needed.
This is about animation in JAVA. I had success when using same dimension on all picture. But if i keep all picture dimension on same size(width and height) i get some bug which when player punch. Before player's hand touch enemy body, enemy died
But others with my case where idle, run, and punch has an different
dimension. Punching animation facing to the left became very strange.
Should his hand hit to the left but his body shifts to the right. This
is because I draw on the x & y are the same.
How can I fix it? Need instructions :D
I use png coz support transparent
I think this can fix with 2 option
1. Fix my collision detection
2. Fix of drawing position my image when some condition occur
Trying to picture your problem, hope this helps.
I am typeing directly from my head, so there are might errors in code
fixing coalision decection
i would try this
Image fist
Image enemy
//in paint
g2D.drawImage(fist,x,y,this);
g2D.drawImage(enemy,x1,y1,this);
Rectangle2D myFist = new Rectangle2D.Double(x,y,fist.getWidth(this),fist.getHeight(this));
Rectangle2D myEnemy = new Rectangle2D.Double(x1,y1,enemy.getWidth(this),enemy.getHeight(this));
if (myEnemy.contains(myFist){
//action u want to happend
}
I think something like this should fix coalision problems
I am seeing this as mario a game on sega
Fix of drawing position
//arm image can be the same image if u want
Image leftArm;
Image rightArm;
image headLegsAndTorsoLeft;
image headLegsAndTorsoRight;
//where am i looking in game if true i look to the leftside of user thats playing
boolean turnedLeft
//in paint
if(turnedLeft){
//this lets it look like he is turned to the left with his right arm in the visible behind his left.
//draw right arm
g2D.drawImage(rightArm,x,y,this);
//draw body moved a bit in x coor
g2D.drawImage(headLegsAndTorsoLeft,x-3,y,this);
// draw left arm a bit more in x coor
g2D.drawImage(leftArm,x-6,y,this);
}else{
//this lets it look like he is turned to the right with his left arm in the visible behind his right.
// draw left arm
g2D.drawImage(leftArm,x,y,this);
//draw body moved a bit in x coor
g2D.drawImage(headLegsAndTorsoRight,x-3,y,this);
//draw right arm a bit more in x coor
g2D.drawImage(rightArm,x-6,y,this);
}
same order for animation of arms, ultimatly i would use different methods animations for torso, leftarm, rightarm
something like keypressed leftarrow torso does walking animation left, hit left arm key moves left arm,hit right arm key moves right arm,thats 3 for lets say left arm, now u need another 3 for when ur char is moved to the right.
Thats how i would try to do things.
I'm writing a ray tracer (using left-handed coordinates, if that makes a difference). It's for the sake of teaching myself the principles, so I'm not using OpenGL or complex features like depth of field (yet). My camera can have an arbitrary position and orientation; I indicate them by way of three vectors, location, look_at, and sky, which behave like the equivalent POV-Ray vectors. Its "film" also has a width and height. (The focal length is implied by the distance from position to look_at.)
My problem is that don't know how to cast the rays. I have two quantities, vx and vy, that indicate where the ray should end up. They both vary from -1 to 1. If they're both -1, I'm casting the ray from the camera's position to the top-left corner of the "film"; if they're both 1, the bottom-right; if they're both 0, the center; and the rest is apparent.
I'm not familiar enough with vector arithmetic to derive an equation for the ray. I would appreciate an explanation of how to do so.
You've described what needs to be done quite well already. Your field of view is determined by the distance between your camera and your "film" that you're going to cast your rays through. The further away the camera is from the film, the narrower your field of view is.
Imagine the film as a bitmap image that the camera is pointing to. Say we position the camera one unit away from the bitmap. We then have to cast a ray though each of the bitmap's pixels.
The vector is extremely simple. If we put the camera location to (0,0,0), and the bitmap film right in front of it with it's center at (0,0,1), then the ray to the bottom right is - tada - (1,1,1), and the one to the bottom left is (-1,1,1).
That means that the difference between the bottom right and the bottom left is (2,0,0).
Assume that your horizontal bitmap resolution should be 1000, then you can iterate through the bottom line pixels as follows:
width = 1000;
cameraToBottomLeft = (-1,1,1);
bottomLeftToBottomRight = (2,0,0);
for (x = 0; x < width; x++) {
ray = cameraToBottomLeft + (x/width) * bottomLeftToBottomRight;
...
}
If that's clear, then you just add an equivalent outer loop for your lines, and you have all the rays that you will need.
You can then add appropriate variables for the distance of the camera to the film and horizontal and vertical resolution. When that's done, you could start changing your look vector and your up vector with matrix transformations.
If you want to wrap your head around computer graphics, an introductory textbook could be of great help. I used this one in college, and I think I liked it.