将工作VB类转换为Java

时间:2017-10-16 23:47:46

标签: plot points

我试图将一个正在运行的VB类转换为Java,但是它给出的结果不正确。

这是VB类:

Public Class Triangle

    Public A As PointF
    Public B As PointF
    Public C As PointF

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sideA">The length of the known side BC</param>
    ''' <param name="sideB">The length of the known side AC</param>
    ''' <param name="sideC">The length of the known side AB</param>
    ''' <param name="angleA">The internal angle in Radians at vertex A</param>
    ''' <param name="angleB">The internal angle in Radians at vertex B</param>
    ''' <param name="angleC">The internal angle in Radians at vertex C</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal sideA As Decimal, ByVal sideB As Decimal, ByVal sideC As Decimal, ByVal angleA As Decimal, ByVal angleB As Decimal, ByVal angleC As Decimal)

        Dim bX As Decimal = CDec(Math.Cos(angleA) * sideC)
        Dim bY As Decimal = CDec(Math.Sin(angleA) * sideC)

        Me.A = PointF.Empty
        Me.C = PointF.Add(A, New SizeF(sideB, 0))
        Me.B = New PointF(bX, -bY)

        If bX < 0 Then
            Me.A = PointF.Add(Me.A, New SizeF(-bX, 0))
            Me.B = PointF.Add(Me.B, New SizeF(-bX, 0))
            Me.C = PointF.Add(Me.C, New SizeF(-bX, 0))
        End If

    End Sub

    Public Sub ScaleToFit(ByVal maxWidthOrHeight As Decimal)
        Dim xCoords() As Single = {Me.A.X, Me.B.X, Me.C.X}

        Dim OverallWidth As Decimal = CDec(xCoords.Max - xCoords.Min)
        Dim OverallHeight As Decimal = CDec(Math.Abs(Me.B.Y))   'B.Y is negative owing to GDI+ coordinates

        Dim scaleFactor As Decimal = If(OverallWidth > OverallHeight, _
                                     maxWidthOrHeight / OverallWidth, _
                                      maxWidthOrHeight / OverallHeight)
        Scale(scaleFactor)
        centreTriangle(25, 300)
    End Sub

    Private Sub Scale(ByVal scaleFactor As Decimal)
        Me.A = ScalePointF(Me.A, scaleFactor)
        Me.B = ScalePointF(Me.B, scaleFactor)
        Me.C = ScalePointF(Me.C, scaleFactor)
    End Sub

    Private Function ScalePointF(ByVal pf As PointF, ByVal factor As Decimal) As PointF
        Return New PointF(pf.X * factor, pf.Y * factor)
    End Function

    Private Sub centreTriangle(ByVal border As Integer, ByVal displaySize As Integer)
        If B.Y > A.Y Then B.Y -= ((B.Y - A.Y) * 2)
        Dim pts() As PointF = New PointF() {A, B, C}
        Dim offset_X As Integer = pts.Min(Function(p) CInt(p.X)) - border
        Dim offset_Y As Integer = pts.Max(Function(p) CInt(p.Y)) - (displaySize - border)
        A = New PointF(A.X - offset_X, A.Y - offset_Y)
        B = New PointF(B.X - offset_X, B.Y - offset_Y)
        C = New PointF(C.X - offset_X, C.Y - offset_Y)
    End Sub     

End Class

这是VB类绘制的三角形:

Triangle plotted with VB class

这是Java类:

public class Triangle
{

    private Point A;
    private Point B;
    private Point C;

    /**
     * @return Point A
     */
    public Point getA()
    {
        return this.A;
    }

    /**
    * @return Point B
    */
    public Point getB()
    {
        return this.B;
    }

    /**
     * @return Point C
     */
    public Point getC()
   {
        return this.C;
   }

  /** 


    @param sideA The length of the known side BC
    @param sideB The length of the known side AC
    @param sideC The length of the known side AB
    @param angleA The internal angle in Radians at vertex A
    @param angleB The internal angle in Radians at vertex B
    @param angleC The internal angle in Radians at vertex C

    */
    public Triangle(float sideA, float sideB, float sideC, float angleA, float angleB, float angleC)
    {

        float bX = (float)(Math.cos(angleA) * sideC);
        float bY = (float)(Math.sin(angleA) * sideC);

        this.A = new Point(0, 0);
        this.C = new Point((int)(this.A.getX() + sideB), (int)this.A.getY());
        this.B = new Point((int)bX, (int)-bY);

        if (bX < 0)
        {
            this.A = new Point((int)(this.A.getX() - bX), (int)this.A.getY());
            this.B = new Point((int)(this.B.getX() - bX), (int)this.B.getY());
            this.C = new Point((int)(this.C.getX() - bX), (int)this.C.getY());            
        }

    }

    public final void ScaleToFit(float maxWidthOrHeight)
    {
        float[] xCoords = {(float)this.getA().getX(), (float)this.getB().getX(), (float)this.getC().getX()};
        float min = 10000;
        float max = -1;

        for(int x = 0; x < 3; x++) {
            if(xCoords[x] < min) {min = xCoords[x];}
            if(xCoords[x] > max) {max = xCoords[x];}
        }

        float OverallWidth = (float)(max - min);
        float OverallHeight = (float)Math.abs(this.getB().getY()); //B.Y is negative owing to Graphics coordinates

        float scaleFactor = (float)(OverallWidth > OverallHeight ? maxWidthOrHeight / OverallWidth : maxWidthOrHeight / OverallHeight);
        Scale(scaleFactor);
        centreTriangle(25, 300);
    }

    private void Scale(float scaleFactor)
    {
        this.A = ScalePoint(this.A, scaleFactor);
        this.B = ScalePoint(this.B, scaleFactor);
        this.C = ScalePoint(this.C, scaleFactor);
    }

    private Point ScalePoint(Point p, float factor)
    {
        return new Point((int)(p.getX() * factor), (int)(p.getY() * factor));
    }

    private void centreTriangle(int border, int displaySize)
    {
        int y1 = (int)this.A.getY();
        int y2 = (int)this.B.getY();
        if(y2 > y1) {y2 -= ((y2 - y1) * 2);}
        this.B = new Point((int)this.B.getX(), y2);

        Point[] pts = new Point[] {this.A, this.B, this.C};
        float min = 10000;
        float max = -1;

        for(int x = 0; x < 3; x++) {
            if(pts[x].getX() < min) {min = (float)pts[x].getX();}
            if(pts[x].getY() > max) {max = (float)pts[x].getY();}
        }

        int offset_X = (int)(min - border);
        int offset_Y = (int)(max - (displaySize - border));
        this.A = new Point((int)(this.A.getX() - offset_X), (int)(this.A.getY() - offset_Y));
        this.B = new Point((int)(this.B.getX() - offset_X), (int)(this.B.getY() - offset_Y));
        this.C = new Point((int)(this.C.getX() - offset_X), (int)(this.C.getY() - offset_Y));
    }

}

这不能正确绘制三角形。这是由Java类绘制的三角形,其输入与VB类相同:

Same triangle plotted with Java class

1 个答案:

答案 0 :(得分:0)

答案是将浮点值转换为int会导致明显的差异。这是固定代码......

package html.triangle;

import java.awt.Point;
import javax.swing.JOptionPane;
import html.shared.points.*;

/**
 *
 * @author Paul Long
 */
public class Triangle
{

    private PointF A;
    private PointF B;
    private PointF C;

    /**
     * @return Point A
     */
    public Point getA()
    {
        return new Point((int)this.A.getX(), (int)this.A.getY());
    }

    /**
     * @return Point B
     */
    public Point getB()
    {
        return new Point((int)this.B.getX(), (int)this.B.getY());
    }

    /**
     * @return Point C
     */
    public Point getC()
    {
        return new Point((int)this.C.getX(), (int)this.C.getY());
    }

    /** 

     @param sideA The length of the known side BC
     @param sideB The length of the known side AC
     @param sideC The length of the known side AB
     @param angleA The internal angle in Radians at vertex A
     @param angleB The internal angle in Radians at vertex B
     @param angleC The internal angle in Radians at vertex C

    */
    public Triangle(float sideA, float sideB, float sideC, float angleA, float angleB, float angleC)
    {

        float bX = (float)(Math.cos(angleA) * sideC);
        float bY = (float)(Math.sin(angleA) * sideC);

        this.A = new PointF(0, 0);
        this.C = new PointF(this.A.getX() + sideB, this.A.getY());
        this.B = new PointF(bX, bY);

        if (bX < 0)
        {
            this.A = new PointF((this.A.getX() + Math.abs(bX)), this.A.getY());
            this.B = new PointF((this.B.getX() + Math.abs(bX)), this.B.getY());
            this.C = new PointF((this.C.getX() + Math.abs(bX)), this.C.getY());            
        }

    }

    public final void ScaleToFit(float maxWidthOrHeight)
    {
        float[] xCoords = {(float)this.A.getX(), (float)this.B.getX(), (float)this.C.getX()};
        float min = 10000;
        float max = -1;

        for(int x = 0; x < 3; x++) {
            if(xCoords[x] < min) {min = xCoords[x];}
            if(xCoords[x] > max) {max = xCoords[x];}
        }

        float OverallWidth = (float)(max - min);

        float[] yCoords = {(float)this.A.getY(), (float)this.B.getY(), (float)this.C.getY()};
        min = 10000;
        max = -1;

        for(int x = 0; x < 3; x++) {
            if(yCoords[x] < min) {min = yCoords[x];}
            if(yCoords[x] > max) {max = yCoords[x];}
        }        

        float OverallHeight = (float)(max - min);

        float scaleFactor = (float)(OverallWidth > OverallHeight ? maxWidthOrHeight / OverallWidth : maxWidthOrHeight / OverallHeight);

        Scale(scaleFactor);
        centreTriangle(25, 300);
    }

    private void Scale(float scaleFactor)
    {
       this.A = ScalePoint(this.A, scaleFactor);
       this.B = ScalePoint(this.B, scaleFactor);
       this.C = ScalePoint(this.C, scaleFactor);
    }

    private PointF ScalePoint(PointF p, float factor)
    {
        return new PointF((p.getX() * factor), (p.getY() * factor));
    }

    private void centreTriangle(int border, int displaySize)
    {
        int y1 = (int)this.A.getY();
        int y2 = (int)this.B.getY();
        if(y2 > y1) {y2 -= ((y2 - y1) * 2);}
        this.B = new PointF(this.B.getX(), y2);

        PointF[] pts = new PointF[] {this.A, this.B, this.C};
        float min = 10000;
        float max = -1;

        for(int x = 0; x < 3; x++) {
            if(pts[x].getX() < min) {min = (float)pts[x].getX();}
            if(pts[x].getY() > max) {max = (float)pts[x].getY();}
        }

        int offset_X = (int)(min - border);
        int offset_Y = (int)(max - (displaySize - border));
        this.A = new PointF((this.A.getX() - offset_X), (this.A.getY() - offset_Y));
        this.B = new PointF((this.B.getX() - offset_X), (this.B.getY() - offset_Y));
        this.C = new PointF((this.C.getX() - offset_X), (this.C.getY() - offset_Y));
    }

}


package html.shared.points;

/**
 *
 * @author Paul Long
 */
public class PointF
{

    private float X;
    private float Y;

    /**
     * @return the X
     */
    public float getX()
    {
        return X;
    }

    /**
     * @return the Y
     */
    public float getY()
    {
        return Y;
    }    

    public PointF(float X, float Y) {
        this.X = X;
        this.Y = Y;
    }

}