3 점으로 표시된 가장 작은 호를 그리는 데 문제가 있습니다. 호 중심, "정박 된"종점 및 반경을 결정하여 호의 다른 쪽 끝을 제공하는 두 번째 점 . 나는 코사인의 법칙을 사용하여 호의 길이를 결정하고 시작 각도로 atan을 사용해 보았지만 호의 시작 위치는 꺼져 있습니다. 자바 그리기 아크 2 점 사이
가 나는 쿼드런트 2 때 (X1, Y1) 앵커 포인트에 잠 호를 얻을 수 있었다, 그러나 그것은 단지 그것을 사분면에있을 때 2.솔루션 나는 모든이를 볼 수 있습니다 작동합니다 if-statements의 뭉치는 서로 상대적인 2 점의 위치를 결정하지만, 내가 간단한 것을 간과한다면 궁금합니다. 어떤 도움이라도 대단히 감사하겠습니다. 여기에서이 같은 generateCurve 방법로부터 포인트 간의 곡선을 갖도록
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.*;
import java.awt.*;
import java.util.*;
class Canvas extends JComponent {
float circleX, circleY, x1, y1, x2, y2, dx, dy, dx2, dy2, radius, radius2;
Random random = new Random();
public Canvas() {
//Setup.
x1 = random.nextInt(250);
y1 = random.nextInt(250);
//Cant have x2 == circleX
while (x1 == 150 || y1 == 150)
{
x1 = random.nextInt(250);
y1 = random.nextInt(250);
}
circleX = 150; //circle center is always dead center.
circleY = 150;
//Radius between the 2 points must be equal.
dx = Math.abs(circleX-x1);
dy = Math.abs(circleY-y1);
//c^2 = a^2 + b^2 to solve for the radius
radius = (float) Math.sqrt((float)Math.pow(dx, 2) + (float)Math.pow(dy, 2));
//2nd random point
x2 = random.nextInt(250);
y2 = random.nextInt(250);
//I need to push it out to radius length, because the radius is equal for both points.
dx2 = Math.abs(circleX-x2);
dy2 = Math.abs(circleY-y2);
radius2 = (float) Math.sqrt((float)Math.pow(dx2, 2) + (float)Math.pow(dy2, 2));
dx2 *= radius/radius2;
dy2 *= radius/radius2;
y2 = circleY+dy2;
x2 = circleX+dx2;
//Radius now equal for both points.
}
public void paintComponent(Graphics g2) {
Graphics2D g = (Graphics2D) g2;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL));
Arc2D.Float centerPoint = new Arc2D.Float(150-2,150-2,4,4, 0, 360, Arc2D.OPEN);
Arc2D.Float point1 = new Arc2D.Float(x1-2, y1-2, 4, 4, 0, 360, Arc2D.OPEN);
Arc2D.Float point2 = new Arc2D.Float(x2-2, y2-2, 4, 4, 0, 360, Arc2D.OPEN);
//3 points drawn in black
g.setColor(Color.BLACK);
g.draw(centerPoint);
g.draw(point1);
g.draw(point2);
float start = 0;
float distance;
//Form a right triangle to find the length of the hypotenuse.
distance = (float) Math.sqrt(Math.pow(Math.abs(x2-x1),2) + Math.pow(Math.abs(y2-y1), 2));
//Law of cosines to determine the internal angle between the 2 points.
distance = (float) (Math.acos(((radius*radius) + (radius*radius) - (distance*distance))/(2*radius*radius)) * 180/Math.PI);
float deltaY = circleY - y1;
float deltaX = circleX - x1;
float deltaY2 = circleY - y2;
float deltaX2 = circleX - x2;
float angleInDegrees = (float) ((float) Math.atan((float) (deltaY/deltaX)) * 180/Math.PI);
float angleInDegrees2 = (float) ((float) Math.atan((float) (deltaY2/deltaX2)) * 180/Math.PI);
start = angleInDegrees;
//Q2 works.
if (x1 < circleX)
{
if (y1 < circleY)
{
start*=-1;
start+=180;
} else if (y2 > circleX) {
start+=180;
start+=distance;
}
}
//System.out.println("Start: " + start);
//Arc drawn in blue
g.setColor(Color.BLUE);
Arc2D.Float arc = new Arc2D.Float(circleX-radius, //Center x
circleY-radius, //Center y Rotates around this point.
radius*2,
radius*2,
start, //start degree
distance, //distance to travel
Arc2D.OPEN); //Type of arc.
g.draw(arc);
}
}
public class Angle implements MouseListener {
Canvas view;
JFrame window;
public Angle() {
window = new JFrame();
view = new Canvas();
view.addMouseListener(this);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 400, 400);
window.getContentPane().add(view);
window.setVisible(true);
}
public static void main(String[] a) {
new Angle();
}
@Override
public void mouseClicked(MouseEvent arg0) {
window.getContentPane().remove(view);
view = new Canvas();
window.getContentPane().add(view);
view.addMouseListener(this);
view.revalidate();
view.repaint();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
; 임의의 중심점이 주어 졌을 때 원의 원주에있는 두 점에 대해 호 길이를 묻는 것입니까? – cabbagery
No.나는 원의 중심과 원 위에 놓인 2 점을 주어서 2 점 사이에서 가장 작은 호를 그리는 방법을 묻습니다. 나는 원점 그리기를 시작할 곳과 아크의 방향 (시계 방향/반 시계 방향)을 바꿔야 하는지를 정말로 찾고있다. –
나는 아직도 그것을 연구 중이다. 그것은 처음에는 눈에 띄게 단순 해 보이지만, 더 심도있게 살펴보면 상당히 복잡해 보이지만 궁극적으로는 상당히 단순하다는 느낌을 떨칠 수 없다. 어쨌든, 나는 까다로운 일이 될 수있는 특정 시나리오를 발견했습니다. 일반화 할 수있는 방법으로 문제를 풀 수 있다면 아직 해결할 수있는 대답을 찾을 수 있습니다. – cabbagery