i have created a 10x10 grid in the center of the screen in android, now i would like to give each square in the grid a coordinate. for example top left square in the grid would be 0 then 1, 2,3 and so on. But i dont know how to do this. i am trying to do this in a draw class which extends view. my code of what i am trying is below
public int coordinates(int posX, int posY){
int startX = (screenWidth / 2) - (rectSide / 2);
int startY = (screenHeight / 2) - (rectSide / 2);
//for(int i=0; i<=10000; i+=100){
xCoord = (startX + (posX*100));
yCoord = (startY + (posY*100));
}
You know you start at point 0,0 top left. So assuming you have equally spaces squares you can just do the screen height / 10 to get how far apart each square should be in the y direction. And then do the same for the x direction. Say your screen was 1000 pixels tall.
Then your grid at position (0,1) would be at (0,100) pixels. (0,2) would be (0,200) you are just multiplying the y coordinate by the height of each square in the grid.
Related
In my Game i want to implement Mouse Controls that control Camera and Player movment.
What i want to have is that when the Mouse moves to the bottom left corner of the window for example, the players Rotation goes towards this Vector.
Also i want the Camera to be shifted in the opposite 2d Vector so it appears that the player, when the cursor is in the bottom left moves into the top right corner.
Like This
And the more the cursor is moved away from the screens middle, the more the player should rotate.
The Camera should also be rotated around the player in order to make it appear that the playermodel is rotated towards the vector it rotates itself in.
This is the function that calculates the camera position:
private void calculateCameraPosition(float horizDistance, float verticDistance)
{
float theta = player.getRotY() + angleAroundPlayer;
float offsetX = (float)((horizDistance ) * Math.sin(Math.toRadians(theta)) );
float offsetZ = (float)(horizDistance * Math.cos(Math.toRadians(theta)));
position.y = player.getPosition().y + verticDistance + 8;
position.x = (player.getPosition().x - offsetX);
position.z = player.getPosition().z - offsetZ ;
this.yaw = 180 - (player.getRotY() + angleAroundPlayer);
}
And i get my Inputs from GFLW, a little bit modified as the Mouse positions, which i subtract from the width/2 and divide into smaller numbers.
angleChange_x = (int) (-(inputHandler.Mouse_x - (Width/2)) /20);
angleChange_y = (int) (-(inputHandler.Mouse_y - (Height/2)) /20 );
I'm trying to draw an arc based on two given points and a given height describing a circle segment. To acomplish this I would use the following method from java.awt.Graphics.
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
First I observe that the x, y, width and height values describes a rectangle containing the circle.
The center of the arc is the center of the rectangle whose origin is (x, y) and whose size is specified by the width and height arguments. (http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html)
So first I start by calculating the x,y,width and height values. The picture below describes how I would do this.
The first picture shows what values I've already got. The arc in the first picture is exactly what I'm trying to draw. In picture number two I calculate the lenght of a line between the two points. My code to perform this step would look like this:
int dx = p1.x- p2.x;
int dy = p1.y - p2.y;
double len = Math.sqrt(Math.pow((double)dx, 2) + Math.pow((double)dy, 2));
As shown in picture three I can now calculate the radius of the circle by using the following function.
radius = h/2 + len^2 / 8h
The first problem I encounter is to calculate the CenterPoint of the circle. This is partly where I need help.
If I was to calculate the Center Point I can then easily find the x, y, whith and height coordinates.
x = centerPoint.x - radius;
y = centerPoint.y - radius;
width = radius * 2;
height = radius * 2;
The last part is to calculate the startAngle and arcAngle based on the values we already calculated.
TL;DR I need help with calculating the angles and the center point.
Thanks in advance!
There's a well-known relationship (see here, for instance) between the chord half length, the height of the arc (also called the sagitta), and the radius. Let the chord length (the distance between p1 and p2 be l = 2 d, let the arc height be h, and let the radius be r. Then
r = (d 2 + h 2) / (2 h)
The center is on the perpendicular bisector of the chord, at a distance r - h from the chord, on the opposite side from the arc.1 You can then use standard inverse trig functions to get the start and end angles for the chord.
1 Note that it is not enough to know p1, p2, and h; you need some way of identifying which side of the chord has the center and which side has the arc.
The applet used is like the first quadrant of a Cartisian Plane with the domain and range (0, 200). My assignment is to draw a house and a sun in this applet.
I am trying to draw the circle for the sun. I really have no idea where to start. We are learning about for loops and nested loops so it probably pertains to that. We haven't got to arrays and general functions like draw.circle do not exist for this applet. If it helps, here is how I drew my roof for the house (two right triangles): Notice it is drawn pixel by pixel. I suspect my teacher wants the same kind of thing for the circle.
//roof
//left side
double starty = 100;
for(double x = 16; x <= 63; x++){
for(int y = 100; y <= starty; y++){
img.set(x, y, JRaster.purple);
}
starty += 1;
}
//right side
double startx = 110;
for(int y = 100; y <= 147; y++){
for(double x = 63; x <= startx; x++){
img.set(x , y, JRaster.purple);
}
startx -= 1;
}
Here's how I would draw the north-east quarter of a circle, pixel by pixel. You can just repeat this with slight variations for the other three quarters. No trigonometry required!
Start by drawing the eastern most point of the circle. Then you'll draw more pixels, moving northwards and westwards, until you get to the northern most point of the circle.
Calculate the distance of the point you've just drawn from the centre. If it's more than the radius, then your next pixel will be one to the left, otherwise, your next pixel will be the one above.
Repeat the previous step till you get to the northern most point.
Post a comment if you get stuck, with converting this to Java, or with adjusting it for the other three quarters of the circle.
I won't give you code, but you should remember how a circle is made. Going from theta=0 to theta=2*pi, the circle is traced by x=cos x, y=sin x.
So, using a for loop that increments a double(here called theta) by something like 0.01 until 2*pi(2*Math.PI or roughly 6.28) plot off Math.cos(theta), Math.sin(theta).
Im trying to draw lines (GWT, Context2d) that are 1 pixel thick, code snippet below:
context.beginPath();
context.setStrokeStyle("rgb(255,0,0)");
context.setLineWidth(1f);
double x = 0;
double gridSize = 10.0f;
while (x < w){
x += gridSize;
context.moveTo(x, 0);
context.lineTo(x, h);
}
context.stroke();
This code draws lines that are at least 2 pixel thick.
Any ideas?
Try to add 0.5 to your coordinates.
Browsers apply antialiasing this may cause blurriness or "2 pixel thick lines".
I'm having a lot of trouble grasping the concept of this example in my text book. The idea is to draw a stoplight with red, yellow and green lights. I have a couple of questions. I'm having trouble figuring out what part of the code does what.
Am I right to assume cx and cy are to figure out the center of the page?
Are fx and fy to figure out the center of the frame?
I don't know what dy does and why it's divided by 4 and not 3 for 3 lights and the LAMP_RADIUS totally confuses me.
On all three add(createFilledCircle) for red, yellow and green I don't understand how their position is calculated inside the stoplight frame.
In the method createFilledCircle() I don't understand GOval circle = newGOval(x-r, y-r, 2 * r, 2 * r);. I don't understand what x-r and y-r does and how that relates to position.
import acm. graphics.*;
import acm. program.*;
import java.awt.*;
public class DrawStoplight extends GraphicsProgram {
public void run () {
double cx = getWidth() / 2;
double cy = getHeight() / 2;
double fx = cx - FRAME_WIDTH / 2;
double fy = cy- FRAME_HEIGHT / 2;
double dy = FRAME_HEIGHT / 4 + LAMP_RADIUS / 2;
GRect frame = new GRect(fx, fy, FRAME_WIDTH, FRAME_HEIGHT);
frame.setFilled(true);
frame.setColor(Color.GRAY);
add(frame);
add(createFilledCircle(cx, cy - dy, LAMP_RADIUS, Color.RED));
add(createFilledCircle(cx, cy, LAMP_RADIUS, Color.YELLOW));
add(createFilledCircle(cx, cy + dy, LAMP_RADIUS, Color.GREEN));
}
private GOval createFilledCircle(double x, double y, double r, Color color){
GOval circle = new GOval(x-r, y-r, 2 * r, 2 * r)
circle.setColor(color);
circle.setFilled(true);
return circle;
}
private static final double FRAME_WIDTH = 50;
private static final double FRAME_HEIGHT = 100;
private static final double LAMP_RADIUS = 10;
}
1. Am I right to assume cx and cy are to figure out the center of the page?
Yes
2. Are fx and fy to figure out the center of the frame?
Not exactly. They are computing the upper left hand corner of the frame. They start at the center and "back up" by half the frame size in each direction.
3. I don't know what dy does and why it's divided by 4 and not 3 for 3 lights and the LAMP_RADIUS totally confuses me.
Look farther down in the code. dy is the vertical distance between lights. The yellow light is drawn exactly at the center, the red is dy above, and the green is dy below. The divisor is 4 because the author chose to align the bottom edge of the red light with a point 1/4 of the frame height from the top of the frame. Similarly he chose to align the top of the green light with a point 1/4 of the frame height from the bottom. He could have chosen many other ways of computing dy.
4. On all three add(createFilledCircle) for red, yellow and green I don't understand how their position is calculated inside the stoplight frame.
They all have the same x-coordinate: the center of the frame. The y-coordinates are computed as explained in 3. Remember in screen coordinates, the positive direction is down, so increasing y puts a light lower. Decreasing puts it higher.
5. In the method createFilledCircle() I don't understand GOval circle = newGOval(x-r, y-r, 2 * r, 2 * r);. I don't understand what x-r and y-r does and how that relates to position.
Go read the manual definition of newGOval. It inscribes an oval shape inside a rectangle. The parameters are the upper left corner of the rectangle followed by the width and height. So if (x,y) is the center, this is giving a box with diagonal (x-r, y-r) to (x+r, y+r). When you inscribe an oval in that, you get a circle centered at (x,y) as desired.
Am I right to assume cx and cy are to figure out the center of the page?
Yes
Are fx and fy to figure out the center of the frame?
No, they're the top-left coordinate
I don't know what dy does and why it's divided by 4 and not 3 for 3 lights and the LAMP_RADIUS totally confuses me.
To fit three lights vertically inside a box you need one in the middle, one at 1/4 height, and one at 3/4 height - hence the division by four. I'm unsure why the LAMP_RADIUS comes into it. It appears to be what I'd normally call a "fudge factor" to make the lamps more widely spaced, i.e a figure that looks right, but without any good reason why it looks right...
On all three add(createFilledCircle) for red, yellow and green I don't understand how their position is calculated inside the stoplight frame.
They're just vertically spaced by dy
In the method createFilledCircle() I don't understand GOval circle = newGOval(x-r, y-r, 2 * r, 2 * r);. I don't understand what x-r and y-r does and how that relates to position.
The GOval puts a circle inside the box defined by the coordinates (x - r, y - r) with size 2r - i.e a square of side-length 2r centered on (x, y)