I am completely lost on how to sort the coordinates into an array, and find the distance between them. This is the question:
Create a new class called “Circle” that can be used to create customized circle objects. Your class should include the following – be sure to comment your class appropriately:
double radius,
double xPosition,
double yPosition, and
A method that computes the distance from the xPosition and yPosition of one circle to the xPosition and yPosition of another circle. Use the standard distance formula to compute this value. You only need to compute the distance from center point to center point for the purposes of this method. Here’s a method header to get you started:
public double distanceFrom(Circle test)
Create a new class called “Assignment06b”. Do the following in this class:
Prompt the user to enter in a number of circles (i.e. How many circles do you want to create?)
Next, ask the user to enter in the radius, xPosition and yPosition for each circle. Store their input in an array of Circles of the appropriate size.
Finally, Iterate through your array and display distance information for each circle. Ensure that you do not calculate the distance from a given circle back to itself (i.e. no need to compute distance between circle #1 and circle #1) — Here’s a sample running of your program.
Here's what I have so far:
import java.util.Scanner;
public class Assignment06b
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("How many circles do you want to create?:");
int amount = input.nextInt();
int[] arrayX = new int [amount];
int[] arrayY = new int [amount];
int counter = 0;
for (counter = 0; counter < amount; counter++)
{
System.out.println("Enter info for Circle #" + (counter + 1));
System.out.print("Radius: ");
double width = input.nextDouble();
System.out.print("X Position: ");
arrayX[counter] = input.nextInt();
System.out.print("Y Position:");
arrayY[counter] = input.nextInt();
}
}
class Circle
{
double radius;
double xPosition;
double yPosition;
Circle(double radius, double xPosition, double yPosition)
{
}
public double distanceFrom(Circle test)
{
double equation = (xPosition-xPosition)*(xPosition-xPosition) + (yPosition-yPosition)*(yPosition-yPosition);
double answer = Math.pow(equation, 0.5);
return answer;
}
}
}
You are tip toeing around object orientation here, change things around so you have an array of circles instead of an array of ints:
Circle[] arrayCircles = new Circle [amount];
Also you aren't setting any values in your circle class, you probably want to fix that:
Circle(double radius, double xPosition, double yPosition)
{
this.radius = radius;
this.xPosition = xPosition;
this.yPosition = yPosition;
}
Then you can add circles to your collection like this:
arrayCirles[0] = new Circle(myRadius, myXPosition, myYPosition);
and call your distanceFrom method call like so:
//Obviously do this in a loop of some kind and make sure they exist first
arrayCircles[0].distanceFrom(arrayCircles[1]);
Hopefully the rest you can figure out yourself
(Also take another look at your distanceFrom method, you want to compare the circle you are passed as a parameter, not to yourself)
I'm currently working in a CAD software and I needed to find the distance between circles (holes) in a square grid/array and I found that the distance between circles (edge to edge) is given by
(L-N*D)/(N-1)
Where:
L is the distance of the array from edge to edge (not to the centre point of the end circle but to the edge of end circles)
N is the number of circles
D is the diameter of the circles
if you measure L from centre point to centre point
(L+D-N*D)/(N-1)
If you're looking to find the distance between centre point to centre point I'm sure you can derive it
Bit late but hopefully this helps someone else!
Related
I have a project where I have to fill a 600x400 window (JavaFX) with 30 random sized circles with no filling. The largest circle must be filled with a translucent red (and if there are multiple large circles with the same radius only one can be filled). I'm able to get all the circles on the screen fine. My problem is getting the largest circle to be red. I haven't been taught arrays which were used in almost all of my many google searches. I cant figure out how exactly to track the largest circle. His hint to us is : "When it comes to keeping track of the largest circle, remember that two reference variables can point to the same Circle object. Maintain a separate Circle reference variable that always points to the largest circle (so far created). You may want to initialize this variable to a circle that has a radius of 0. You can get the radius of a circle using the getRadius method." I created a circle object and a largestCircle object but don't understand how to make the largestCircle object have the highest radius.
This is the code I have so far:
{
Random gen = new Random();
int x = 0;
int y = 0;
int radius = 0;
double largestRadius = Math.max(radius);
Circle largestCircle = null;
Group root = new Group();
//prints out 30 circles
for (int i = 0; i <= 30; i++)
{
Circle circle = new Circle(x, y, radius);
{
radius = gen.nextInt(66) + 10; //generates random radius from 10 to 75
x = gen.nextInt(600 - 2 * radius) + radius;
y = gen.nextInt(400 - 2 * radius) + radius;
}
if (circle.getRadius() == largestRadius)
{
largestCircle = circle;
largestCircle.setFill(Color.rgb(255, 0, 0, 0.3));
}
circle.setFill(null);
circle.setStroke(Color.rgb(gen.nextInt(256), + gen.nextInt(256), gen.nextInt(256)));
circle.setStrokeWidth(3);
root.getChildren().add(circle);
}
after I generate the random circles how to I find the max radius that was generated and set it to largestCircle? the highest radius a circle can be is 75, but sometimes none of the circles have a radius of 75. How do I set the max to be the highest number the program randomly generates?
Any help would be greatly appreciated! Thank you for your time
How about the following.
It has a two fixes.
-1, use > and not == when figuring if current circle is the largest.
-2, change the color of the largest circle at the end, after all the circles have been made... else you might make multiple circles red.
{
Random gen = new Random();
int x = 0;
int y = 0;
int radius = 0;
double largestRadius = Math.max(radius);
Circle largestCircle = null;
Group root = new Group();
//prints out 30 circles
for (int i = 0; i <= 30; i++)
{
Circle circle = new Circle(x, y, radius);
if (circle.getRadius() > largestRadius)
{
largestCircle = circle;
}
{
radius = gen.nextInt(66) + 10; //generates random radius from 10 to 75
x = gen.nextInt(600 - 2 * radius) + radius;
y = gen.nextInt(400 - 2 * radius) + radius;
}
circle.setFill(null);
circle.setStroke(Color.rgb(gen.nextInt(256), + gen.nextInt(256), gen.nextInt(256)));
circle.setStrokeWidth(3);
root.getChildren().add(circle);
}
largestCircle.setFill(Color.rgb(255, 0, 0, 0.3));
It is generally a good idea to initialize any max variable with a small number that is out of scope for your project. In this case, since radius can not be -1, I would do
double largestRadius = -1;
After this, it doesn't matter how big the radius can be, any radius bigger than -1 will change the largestRadius.
It looks to me like you are only missing one part and that is the if the newly created circle has a radius > largestRadius.
if(circle.getRadius() > largestRadius){
largestCircle = circle;
largestRadius = circle.getRadius();
}
After this, you have checked for if the new circle has a radius greater than AND you have checked if the new circle has a radius equal to. Keeping the if statement that you already have, you will always reference the newest circle with the largestRadius.
I would keep the circle objects in an array. Use a double (or whatever number type is appropriate for your random values) to track the high value with a simple comparison (is my current high value less than the new random value? if so, update high value) each time you generate a random value and create a circle of that size.
Once you have your 30 circles in your array simply loop through it until you find the first occurrence of your high value, when you find it make that circle whatever color.
Circle[] myCircles=new Circle[30];
double largestCircle;
for(int i=0;i<30;i++){
// determine your x,y, and radius here
myCircles[i]=new Circle(x,y,radius);
if(radius>largestCircle) largestCircle=radius;
}
Then to loop thru your myCircles and do things with each one
for(int i=0;i<30;i++){
if(myCircles[i].getRadius()==largestCircle){
// make myCircles[i] red here
}
}
I have been assigned the following task for an introductory java course:
You should write a class that represents a circle object and includes the following:
Private class variables that store the radius and centre coordinates of the object.
Constructors to create circle objects with nothing supplied, with just a radius value supplied and with a radius and centre coordinates supplied.
Public instance methods that allow the radius and centre coordinates to be set and retrieved (often known as set/get methods).
Public instance methods that return the circumference and area of the circle.
A public class method that tests if two circle objects overlap or not
Here is my code:
import java.lang.Math;
public class Circle {
private double xCentre, yCentre, Radius;
// constructors
public Circle() {
xCentre = 0.0;
yCentre = 0.0;
Radius = 1.0;
}
public Circle(double R) {
xCentre = 0.0;
yCentre = 0.0;
Radius = R;
}
public Circle(double x, double y, double R) {
xCentre = x;
yCentre = y;
Radius = R;
}
//getters
public double getX() {
return xCentre;
}
public double getY() {
return yCentre;
}
public double getRadius() {
return Radius;
}
//setters
public void setX(double NewX) {
xCentre = NewX;
}
public void setY(double NewY) {
yCentre = NewY;
}
public void setRadius(double NewR) {
Radius = NewR;
}
//calculate circumference and area
public double Circumference() {
return 2*Math.PI*Radius;
}
public double Area() {
return Math.PI*Radius*Radius;
}
//determine overlap
public static double Overlap(Circle c1, Circle c2) {
double xDelta = c1.getX() - c2.getX();
double yDelta = c1.getY() - c2.getY();
double separation = Math.sqrt(xDelta*xDelta + yDelta*yDelta);
double radii = c1.getRadius() + c2.getRadius();
return separation - radii;
}
}
}
and
import java.io.Console;
public class cp6 {
public static void main(String args[]){
//Set up the Console
Console myConsole = System.console();
//Declare cirlce
Circle first = new Circle(2.0,4.0,6.0);
myConsole.printf("Circumference of first circle is ", first.Circumference(), "\n");
myConsole.printf("Area of first circle is ", first.Circumference(), "/n");
first.setRadius(2);
first.setX(2);
first.setY(2);
myConsole.printf("New X of first circle is ", first.getX(), "/n");
myConsole.printf("New Y of first circle is ", first.getY(), "/n");
myConsole.printf("New Radius of first circle is ", first.getRadius(), "/n");
Circle second = new Circle(-1.0,3.0,5.0);
Circle third = new Circle(1,1,1);
if (Circle.Overlap(second, third) <= 0) {
myConsole.printf("Second and third circles overlap");
}
else {
myConsole.printf("Second and third circles do not overlap");
}
myConsole.printf("New Y of first circle is ", first.getY());
Calculate and print out distance between them using the class method
myConsole.printf("Distance between first and second is : %.5g\n", Circle.Overlap(first, second));
}
}
The second program just has to demonstrate each aspect addressed in the brief I pasted at the top and I've only a rough idea of how to do this so if what I'm doing seems stupid to any of you please offer suggestions of what else I can do.
Your problem is that you're using the Console.printf() method incorrectly.
The first parameter to this method should be a format, and it has to have placeholders inside it for the other parameters. Read up on it in The Java Platform documentation. In fact, you should familiarize yourself with the Java platform documentation. You need to use it often to make sure you're calling methods correctly or what methods are available in a given class.
So, your printout lines should actually have been:
myConsole.printf("Circumference of first circle is %.2f%n", first.Circumference());
myConsole.printf("Area of first circle is %.2f%n", first.Area());
...etc.
The format %.2f means "The corresponding parameter is a floating-point number. Display it with a precision of 2 digits after the decimal point". The %n replaces your "\n" - the whole "template" of the print should be just in the format string. And in this type of format, one should use %n instead of \n.
I'm not sure why you opted for using the system console rather than the usual System.out.println(). If you choose to go with System.out, there is also a printf() method there that works exactly as Console.printf() - the first parameter is a format, the others are embedded in it.
One last comment: there are conventions when writing Java code:
Indent your code properly
Class names' first letter is always uppercase.
Non-constant fields and local variable names' first letter is always lowercase.
Method names also start with a lowercase letter.
Hello I was given an assignment in which I have to draw 3 circles using their center coordinates and radii which is input by the user. The assignment specifically states that I must input the center and radius, I cannot input the x and y coordinates separately. They must be input as a coordinate pair (x,y).
Here is my code.
import java.util.*;
import java.awt.*;
public class Circles {
static Scanner CONSOLE = new Scanner(System.in);
public static final int PANEL_HEIGHT = 300;
public static final int PANEL_WIDTH = 400;
public static void main (String [] args) {
DrawingPanel panel = new DrawingPanel (PANEL_HEIGHT,PANEL_WIDTH);
Graphics g = panel.getGraphics();
System.out.println();
System.out.println("Red Circle data");
System.out.println("Input center of Red circle: ");
int center1 = CONSOLE.nextInt();
System.out.println("Input radius of Red circle: ");
int radius1 = CONSOLE.nextInt();
System.out.println();
System.out.println("Blue Circle data");
System.out.println("Input center of Blue circle: ");
int center2 = CONSOLE.nextInt();
System.out.println("Input radius of Blue circle: ");
int radius2 = CONSOLE.nextInt();
System.out.println();
System.out.println("Green Circle data");
System.out.println("Input center of Green circle: ");
int center3 = CONSOLE.nextInt();
System.out.println("Input radius of Green circle: ");
int radius3 = CONSOLE.nextInt();
g.setColor(Color.RED);
g.fillOval(center1 -radius1 ,center1-radius1 ,radius1 ,radius1);
g.setColor(Color.BLUE);
g.fillOval(center2 -radius2 ,center2-radius2 ,radius2 ,radius2);
g.setColor(Color.GREEN);
g.fillOval(center3 -radius3 ,center3-radius3 ,radius3 ,radius3);
}
}
My problem is that I don't know how to properly input and store the x and y coordinates as one int. As it is currently it just takes one data point, stores it, and uses it as both x and y.
System.out.println("Red Circle data");
System.out.println("Input center of Red circle: ");
int center1 = CONSOLE.nextInt();
Here is where I know java must ask for the values and store them.
g.setColor(Color.RED);
g.fillOval(center1(THIS SHOULD BE THE X VALUE) -radius1 ,center1(THIS SHOULD BE THE Y VALUE)-radius1 ,radius1 ,radius1);
Then here I have to somehow get the values stored in the above code.
Please help i'm kinda new to this and would appreciate some feedback! Hope what im asking for makes sense! :S
not JAVA coder so I stick to C++, are you sure you want x,y pair in single int ?
1.If yes then just encode it
but you must know the coordinates range
for example if you have 32bit int and your coordinates fits to 16 bit then:
int xy,x,y;
xy = (x&0x0000FFFF) | ((y&0x0000FFFF)<<16); // this makes xy = (x,y)
and if you need to unpack the data again do:
x= xy &0x0000FFFF; if (int(x&00008000)!=0) x|=0xFFFF0000;
y=(xy>>16)&0x0000FFFF; if (int(y&00008000)!=0) y|=0xFFFF0000;
the if at the end restores the missing bits for negative numbers ...
2.if in single variable instead then use
array:
int xy[2],x,y;
xy[0]=x;
xy[1]=y;
or struct/class
int x,y;
struct point { int x,y; } xy;
xy.x=x;
xy.y=y;
I want a java function where if we pass the lat,long of a object, whether that object lies inside a area. Again this Area is defined by lat,long
for example I define a area with 3 lat/long positions.( a rectangle ) and if I pass a lat/long position it should return me whether its within the rectangle.
If the area is always a rectangle, the easiest way is to compare the coordinates.
Let's assume that your rectangle is defined by its upper left (r1x: lon and r1y: lat) and lower right (r2x and r2y) corners. Your object (a point) is defined by px: lon and py: lat.
So, your object is inside the area if
px > r1x and px < r2x
and
py < r1y and py > r2y
Programatically it would be something like:
boolean isPInR(double px, double py, double r1x, double r1y, double r2x, double r2y){
if(px > r1x && px < r2x && py < r1y && py > r2y){
//It is inside
return true;
}
return false;
}
EDIT
In the case where your polygon is not a rectangle, you can use the Java.awt.Polygon Class. In this class you will find the method contains(x,y) which return true if the point with x and y coordinates is inside the Polygon.
This method uses the Ray-casting algorithm. To simplify, this algorithm draw a segment in a random direction from your point. If the segment cross your polygon's boarder an odd number of times, then it is inside your polygon. If it crosses it an even number of times, then it is outside.
To use the polygon Class, you can do something like:
//This defines your polygon
int xCoord[] = {1,2,3,5,9,-5};
int yCoord[] = {18,-32,1,100,-100,0};
myPolygon = new Polygon(xCoord, yCoord, xCoord.length);
//This finds if the points defined by x and y coordinates is inside the polygon
Boolean isInside = myPolygon.contains(x,y);
And don't forget to
import java.awt.Polygon;
EDIT
Right coordinates are in Double !
So you need to use Path2D.Double !
Begin by import java.awt.geom.Path2D;
Let's say you start with similar arrays as before:
//This defines your polygon
Double xCoord[] = {1.00121,2,3.5464,5,9,-5};
Double yCoord[] = {18.147,-32,1,100,-100.32,0};
Path2D myPolygon = new Path2D.Double();
//Here you append all of your points to the polygon
for(int i = 0; i < xCoord.length; i++) {
myPolygon.moveTo(xCoord[i], yCoord[i]);
}
myPolygon.closePath();
//Now we want to know if the point x, y is inside the Polygon:
Double x; //The x coord
Double y; //The y coord
Boolean isInside = myPolygon.contains(x,y);
And here you go with Double !
I took a course in Java at University 2 years ago, and now I have to create a program where I type in 2 sets of coordinates, and the program will create a 3-D graph of a line between the two points, as well as give the slope and angle between the two points. I've been trying to get familiar with the Java syntax, but I have to complete the program by tomorrow so I figured I would ask for help. I've put together code, but it isn't in Java's syntax (most of it isn't anyways) and I need help converting it to code that will work. I'm using double for my variables because the points the user enters can be decimals. The only coordinates the user will input are x, and y for both points, the z coordinates are set at z1 = 0 and z2 = 1. The way I have put the variables together, it assumes the z coordinate extends out of the screen, and the y coordinate plane extends vertically.
Again, I'm familiar with general coding terms, but as I've looked around on the internet i see things like import java.util.* and that stuff doesn't make sense to me as to how it owuld apply to my program.
Any help is appreciated!
P.S. if you want me to add comments as to what certain things mean, let me know. i.e. create.cube is a syntax I made up, but I want the computer to create a window that will show a cube with the coordinates I have set.
package slope;
public class Slope1 {
double xvar;
double yvar;
double zvar;
xvar x1 = new xvar;
xvar x2 = new xvar;
yvar y1 = new yvar;
yvar y2 = new yvar;
zvar z1 = new zvar;
zvar z2 = new zvar;
public static void main(String[]args){
z1 = 0;
z2 = 1;
get.x1 from user
if(x1>9 or x1<-9){
System.out.println("Please choose values within range")
}
get.x2 from user
if(x2>9 or x2<-9){
System.out.println("Please choose values within range")
}
get.y1 from user
if(y1>12 or y2<-12){
System.out.println("Please choose values within range")
}
get.y2 from user
if(y1>12 or y2<-12){
System.out.println("Please choose values within range")
}
slope1 = (y2-y1)/(x2-x1);
angle1 = arctan(slope1);
distance1 = (y2-y1)/sin(angle1);
slopeFinal = 1/distance1;
angleFinal = arctan(slopeFinal);
System.out.println("Your Slope is " + slopeFinal);
System.out.println("Angle of entry is " + angleFinal);
}
public static void main(String[]args){
create.cube;
xlength cube = -9 to 9;
ylength cube = -12 to 12;
zlenght cube = 0 to 12
cube x origin at x=0;
cube y origin at y=0;
cube z origin at z=0;
draw line from (x1,y1,z1) to (x2,y2,z2) in cube;
}
}
Your code doesn't make sense at all and has many syntax errors.
create.cube;
xlength cube = -9 to 9;
ylength cube = -12 to 12;
zlenght cube = 0 to 12
cube x origin at x=0;
cube y origin at y=0;
cube z origin at z=0;
None of the above are valid statements in java.
You might wanna study java once again and look up gui programming with java as well. below is a link to get you started.
https://en.wikibooks.org/wiki/Java_Programming