Java: 질량 중심을 중심으로 삼각형 회전 (Java: Rotate triangle around mass center)


문제 설명

Java: 질량 중심을 중심으로 삼각형 회전 (Java: Rotate triangle around mass center)

질량 중심을 중심으로 삼각형을 회전해야 하는 방법을 만들고 있는데 작동해야 하지만 확인 결과 작동하지 않는 것으로 나타났습니다. 정확한 회전이 아닐 수도 있다는 것을 이해하지만 삼각형의 회전 후에도 질량 중심인 점은 동일하게 유지되어야 합니다.

내가 무엇을 잘못했나요?

    public void rotateAroundMassCenter(double grad){
    System.out.println("Starting...");

    Point initial = this.massCenter();

    Point o =this.massCenter();
    o.printPoint();
    double angle=Math.toRadians(grad);
    System.out.println();

    //formulas
    //    x' = x0+(x‑x0)*cos(A)+(y0‑y)*sin(alpha);
    //    y' = y0+(x‑x0)*sin(A)+(y‑y0)*cos(alpha);

    //new points
    int ax = (int) (o.x+(this.getA().x‑o.x)*Math.cos(angle)‑(this.getA().y‑o.y) *Math.sin(angle));
    int ay = (int) (o.y+(this.getA().x‑o.x)*Math.sin(angle)+(this.getA().y‑o.y) * Math.cos(angle));
    this.a.movePoint(ax, ay);

    int bx = (int) (o.x+(this.getB().x‑o.x)*Math.cos(angle)‑(this.getB().y‑o.y) *Math.sin(angle));
    int by = (int) (o.y+(this.getB().x‑o.x)*Math.sin(angle)+(this.getB().y‑o.y) * Math.cos(angle));
    this.b.movePoint(bx, by);

    int cx = (int) (o.x+(this.getC().x‑o.x)*Math.cos(angle)‑(this.getC().y‑o.y) *Math.sin(angle));
    int cy = (int) (o.y+(this.getC().x‑o.x)*Math.sin(angle)+(this.getC().y‑o.y) * Math.cos(angle));
    this.c.movePoint(cx, cy);

    Point finalCenter=this.massCenter();
    System.out.println();
    finalCenter.printPoint();

    //check center position after rotate
    if (initial==finalCenter  ){
        System.out.println("Rotation was correct");
    }
    else{
        System.out.println("Algorytm is wrong");
    }
    this.print();
}

여기에 내가 찾는 방법이 있습니다. 질량 중심:

// mass centre
    public Point massCenter(){
        double x = (this.getA().x+this.getB().x+this.getC().x)/3;
        double y = (this.getA().y+this.getB().y+this.getC().y)/3;
        Point o= new Point(x,y);
        return o;
    }

결과:

Ready to go
Triangle is: 
A:(1.0; 1.0)B: (3.0; 1.0)C: (1.0; 4.0)
Starting...
(1.6666666666666667;2.0)Old center
New center
(3.0;3.3333333333333335)Algorithm is wrong
Triangle is: 
A:(2.0; 1.0)B: (6.0; 3.0)C: (1.0; 6.0)

참조 솔루션

방법 1:

very little mistake ^^

if (initial==finalCenter  ){...

that's never true, you must compare by using equal:

if (initial.equals(finalCenter)  ){...

(by Bodja SlyMartin Frank)

참조 문서

  1. Java: Rotate triangle around mass center (CC BY‑SA 2.5/3.0/4.0)

#geometry #java






관련 질문

이미지를 사용하지 않고 Nx2 행렬에서 원의 반지름을 어떻게 찾을 수 있습니까? (How could I find the radius of a circle in a Nx2 matrix, not using images)

d3.js의 궤도 유형 차트 (Orbit type chart in d3.js)

원의 둘레에 스프라이트 추가 (Add a sprite on circumference of a circle)

Java: 질량 중심을 중심으로 삼각형 회전 (Java: Rotate triangle around mass center)

값이 없는 경우 HTML5 로컬 저장소 개체는 무엇을 반환합니까? (What does the HTML5 Local Storage object return if there is no value?)

WordPress에서 Simple Jquery Click이 작동하지 않음 (Simple Jquery Click not working in WordPress)

직사각형 필드 내에서 다양한 크기의 직사각형을 효율적으로 배치 (Efficient placement of variable size rectangles within a rectangular field)

{'northeast': {'lat':}, 'southwest': {'lat': }}(Google 지도) Python에서 다각형을 만드는 방법 (How to create Polygon out of {'northeast': {'lat':}, 'southwest': {'lat': }} (google maps) Python)

실린더 슬라이스를 따라 두 점 사이의 SVG 경로 호를 계산하는 방법 (How to calculate SVG path arc between two points along the slice of a cylinder)

make_solid() PostGIS를 사용하여 꼭짓점 테이블에서 polyhedralsurfaceZ 생성 (Create polyhedralsurfaceZ from vertex table with make_solid() PostGIS)

시작/끝 각도 문제가 있는 OpenGL 타원 (OpenGL Ellipse with start/end angle issues)

2D 그리드의 두 선분이 인접하는지 테스트하는 알고리즘 (Algorithm to test if two line segments on a 2d grid are adjacent)







코멘트