⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coordinatesys.java

📁 使用JAVA实现的三维图形处理.可根据输入的曲线方程,如x^2+y^2+z^2=1表示为(1-(Y/50)^2+(Z/60)^2)^(1/2)*100),绘制三维曲线.并可通过拖动鼠标让曲线在三维空间
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                switch(nSymmetry)
                {
                case Symmetry.SYMMETRY_X_2:	m_Append(axis.m_GetWindowPoint(-x,y,z));	break;
                case Symmetry.SYMMETRY_Y_2:	m_Append(axis.m_GetWindowPoint(x,-y,z));	break;
                case Symmetry.SYMMETRY_Z_2:	m_Append(axis.m_GetWindowPoint(x,y,-z));	break;
                default:;
                }			
            }
            else m_Append(new Point(-1,-1));
	}
    }
    public  Point	m_GetAt(int nPoint)
    {
	if( nPoint > m_nValid ) return null;		//nPoint超出有效范围
	if( m_nX[nPoint] == -1 && m_nY[nPoint] == -1 ) return null;
	return new Point(m_nX[nPoint],m_nY[nPoint]);
    }
    public  int		m_GetCount() { return m_nValid; }
    public  int[]       m_GetXs() { return m_nX; }
    public  int[]       m_GetYs() { return m_nY; }
};

public class CoordinateSys
{
    private CCoordinateAxis	m_axis = new CCoordinateAxis();
    private String		m_szExpress;
    private String		m_szFormulaX;
    private String		m_szFormulaY;
    private String		m_szFormulaZ;
    private ArrayList           m_oCurvesYx = new ArrayList();	//Y0X平面上的所有区线
    private ArrayList		m_oCurvesXz = new ArrayList();	//X0Z平面上的所有区线
    private ArrayList		m_oCurvesZy = new ArrayList();	//ZOY平面上的所有区线
    private ArrayList		m_oTraceYx = new ArrayList();	//窗口Y0X平面上的所有区线
    private ArrayList		m_oTraceXz = new ArrayList();	//窗口X0Z平面上的所有区线
    private ArrayList		m_oTraceZy = new ArrayList();	//窗口ZOY平面上的所有区线
    private Validate            m_oValidat = new Validate();
    private ViewRange		m_oViewRange = new ViewRange();
    private void		m_ClearCurve()
    {
	m_oCurvesYx.clear();
	m_oCurvesXz.clear();
	m_oCurvesZy.clear();
    }
    private void		m_ClearTrace()
    {
        m_oTraceYx.clear();
        m_oTraceXz.clear();
        m_oTraceZy.clear();
    }
    private void		m_CalualateX(double y) 
    {
	double  xx          = Math.sqrt(-1.0);	// INF value
	double  x           = 0.0f;
	int	nSpace      = (int)(Math.abs(m_oViewRange.m_fEnd - m_oViewRange.m_fBegin)/m_oViewRange.m_fStep + 2);
	int	nSymmetry   = 0;

	switch( m_oValidat.m_nX )
	{
	case Validate.VALIDATE_ONE:	nSymmetry = Symmetry.SYMMETRY_ONE;	break;
	case Validate.VALIDATE_TWO:	nSymmetry = Symmetry.SYMMETRY_X_2;	break;
	case Validate.VALIDATE_NON:
	default:			return;
	}
	CCurvePoints	pCurve	= new CCurvePoints(nSpace,nSymmetry);
	for( double z = m_oViewRange.m_fBegin; z <= m_oViewRange.m_fEnd; z += m_oViewRange.m_fStep )
	{	//如果所有值都无效,则放弃该曲线,释放刚申请的内存
		Calculater		cal = new Calculater();
                ErrorInfo               oError = new ErrorInfo();;
		x = cal.m_Calculate( m_szFormulaX,0,y,z,oError );
		if( !Double.isNaN(x) || !Double.isNaN(xx) )
		{	//当前一个值有效时,记录当前无效值是为了绘图时知道曲线在那里断开
			pCurve.m_Append( x,y,z );	//当前值有效或前一个值有效
			xx = x;						//保存当前值,无论其是否有效
		}	//当前值和前一个值都无效,忽略
	}
	if( pCurve.m_GetCount() > 0 ) m_oCurvesXz.add(pCurve);
    }
    private void		m_CalualateY(double z)
    {
	double  yy          = Math.sqrt(-1.0);	// INF value
	double  y           = 0.0f;
	int	nSpace      = (int)(Math.abs(m_oViewRange.m_fEnd - m_oViewRange.m_fBegin)/m_oViewRange.m_fStep + 2);
	int	nSymmetry   = 0;

	switch( m_oValidat.m_nY )
	{
	case Validate.VALIDATE_ONE:	nSymmetry = Symmetry.SYMMETRY_ONE;	break;
	case Validate.VALIDATE_TWO:	nSymmetry = Symmetry.SYMMETRY_Y_2;	break;
	case Validate.VALIDATE_NON:
	default:			return;
	}
	CCurvePoints	pCurve	= new CCurvePoints(nSpace,nSymmetry);
	for( double x = m_oViewRange.m_fBegin; x <= m_oViewRange.m_fEnd; x += m_oViewRange.m_fStep )
	{	//如果所有值都无效,则放弃该曲线,释放刚申请的内存
		Calculater		cal = new Calculater();
                ErrorInfo               oError = new ErrorInfo();;
		y = cal.m_Calculate( m_szFormulaY,x,0,z,oError );
		if( !Double.isNaN(y) || !Double.isNaN(yy) )
		{	//当前一个值有效时,记录当前无效值是为了绘图时知道曲线在那里断开
			pCurve.m_Append( x,y,z );	//当前值有效或前一个值有效
			yy = y;						//保存当前值,无论其是否有效
		}	//当前值和前一个值都无效,忽略
	}
	if( pCurve.m_GetCount() > 0 ) m_oCurvesYx.add(pCurve);
    }
    private void		m_CalualateZ(double x)
    {
	double  zz          = Math.sqrt(-1.0);	// INF value
	double  z           = 0.0f;
	int	nSpace      = (int)(Math.abs(m_oViewRange.m_fEnd - m_oViewRange.m_fBegin)/m_oViewRange.m_fStep + 2);
	int	nSymmetry   = 0;

	switch( m_oValidat.m_nZ )
	{
	case Validate.VALIDATE_ONE:	nSymmetry = Symmetry.SYMMETRY_ONE;	break;
	case Validate.VALIDATE_TWO:	nSymmetry = Symmetry.SYMMETRY_Z_2;	break;
	case Validate.VALIDATE_NON:
	default:			return;
	}
	CCurvePoints	pCurve	= new CCurvePoints(nSpace,nSymmetry);
	for( double y = m_oViewRange.m_fBegin; y <= m_oViewRange.m_fEnd; y += m_oViewRange.m_fStep )
	{	//如果所有值都无效,则放弃该曲线,释放刚申请的内存
		Calculater		cal = new Calculater();
                ErrorInfo               oError = new ErrorInfo();;
		z = cal.m_Calculate( m_szFormulaZ,x,y,0,oError );
		if( !Double.isNaN(z) || !Double.isNaN(zz) )
		{	//当前一个值有效时,记录当前无效值是为了绘图时知道曲线在那里断开
			pCurve.m_Append( x,y,z );	//当前值有效或前一个值有效
			zz = z;						//保存当前值,无论其是否有效
		}	//当前值和前一个值都无效,忽略
	}
	if( pCurve.m_GetCount() > 0 ) m_oCurvesZy.add(pCurve);
    }
    public  CoordinateSys()
    {
    }
    public CCoordinateAxis	m_GetCoordinate()
    {
        return m_axis;
    }
    public void		m_SetExpress(String szExpress)  { m_szExpress = szExpress; }
    public void		m_GetExpress(String szExpress)  { szExpress = m_szExpress; }
    public void		m_SetFormula(String szFormulaX,String szFormulaY,String szFormulaZ)
    {
	m_szFormulaX = szFormulaX;
	m_szFormulaY = szFormulaY;
	m_szFormulaZ = szFormulaZ;
    }
    public void		m_GetFormula(String szFormulaX,String szFormulaY,String szFormulaZ)
    {
	szFormulaX = m_szFormulaX;
	szFormulaY = m_szFormulaY;
	szFormulaZ = m_szFormulaZ;
    }
    public void		m_SetValidate(Validate oValidate)
    {
	m_oValidat = oValidate;
    }
    public Validate	m_GetValidate()
    {
        return m_oValidat;
    }
    public void		m_SetResolution(int nResolution)
    {
	if( nResolution > 0 )
	{
            m_oViewRange.m_fStep = Math.abs(m_oViewRange.m_fEnd-m_oViewRange.m_fBegin)/nResolution;
	}
    }
    public void		m_SetViewRange(double fBegin,double fEnd,int nResolution)
    {
	m_oViewRange.m_fBegin = fBegin;
	m_oViewRange.m_fEnd = fEnd;
	if( nResolution > 0 )
	{
            m_oViewRange.m_fStep = Math.abs(m_oViewRange.m_fEnd-m_oViewRange.m_fBegin)/nResolution;
	}
    }
    public int		m_GetViewRange(ViewRange viewRange)
    {
        viewRange = m_oViewRange;
        int nResolution = (int)(Math.abs(viewRange.m_fEnd-viewRange.m_fBegin)/viewRange.m_fStep);     
        return nResolution;
    }
    public void		m_Calualate(String szFormulaX,String szFormulaY,String szFormulaZ,
                                    Validate oValidate,boolean b3D)
    {
	m_SetFormula(szFormulaX,szFormulaY,szFormulaZ);
	m_SetValidate(oValidate);
	m_ClearCurve();
	m_CalualateX(0);
	m_CalualateY(0);
	m_CalualateZ(0);
	if( b3D )
	{
            for( double zx = m_oViewRange.m_fBegin; zx <= m_oViewRange.m_fEnd; zx += m_oViewRange.m_fStep ) m_CalualateX(zx);
            for( double zy = m_oViewRange.m_fBegin; zy <= m_oViewRange.m_fEnd; zy += m_oViewRange.m_fStep ) m_CalualateY(zy);
            for( double xy = m_oViewRange.m_fBegin; xy <= m_oViewRange.m_fEnd; xy += m_oViewRange.m_fStep ) m_CalualateZ(xy);
	}
    }
    public void		m_PreDraw()
    {
	m_ClearTrace();
	for( int n = 0; n < m_oCurvesYx.size(); n ++ )
	{
		CCurvePoints	pCurve = (CCurvePoints)m_oCurvesYx.get(n);
		CTracePoints	pTrace = new CTracePoints(pCurve,m_axis);
		if( pTrace.m_GetCount()>0 ) m_oTraceYx.add(pTrace);
	}
	for( int n = 0; n < m_oCurvesXz.size(); n ++ )
	{
		CCurvePoints	pCurve = (CCurvePoints)m_oCurvesXz.get(n);
		CTracePoints	pTrace = new CTracePoints(pCurve,m_axis);
		if( pTrace.m_GetCount()>0 ) m_oTraceXz.add(pTrace);
	}
	for( int n = 0; n < m_oCurvesZy.size(); n ++ )
	{
		CCurvePoints	pCurve = (CCurvePoints)m_oCurvesZy.get(n);
		CTracePoints	pTrace = new CTracePoints(pCurve,m_axis);
		if( pTrace.m_GetCount()>0 ) m_oTraceZy.add(pTrace);
	}
    }
    public void		m_Draw(Graphics graphics)
    {
	m_axis.m_DrawCoordinate(graphics);
	Color	colorX = new Color(127,127,127);
	Color	colorY = new Color(192,192,192);
	Color	colorZ = new Color(255,255,255);

        graphics.setColor(colorX);
	for( int m = 0; m < m_oTraceXz.size(); m ++ )
	{
            Point           pointLast = null;
            CTracePoints    pTrace = (CTracePoints)m_oTraceXz.get(m);
            int             nCount = pTrace.m_GetCount();
            for( int n = 0; n < nCount; n ++ )
            {
                Point   pointCurr = pTrace.m_GetAt(n);
                if( pointLast != null && pointCurr != null )
                {//当前点与前一点不连续
                    graphics.drawLine(pointLast.x,pointLast.y,pointCurr.x,pointCurr.y);
                }
                pointLast = pointCurr;
            }
	}
        graphics.setColor(colorY);
	for( int m = 0; m < m_oTraceYx.size(); m ++ )
	{
            Point		pointLast = null;
            CTracePoints	pTrace = (CTracePoints)m_oTraceYx.get(m);
            int             nCount = pTrace.m_GetCount();
            for( int n = 0; n < nCount; n ++ )
            {
                Point   pointCurr = pTrace.m_GetAt(n);
                if( pointLast != null && pointCurr != null )
                {//当前点与前一点不连续
                    graphics.drawLine(pointLast.x,pointLast.y,pointCurr.x,pointCurr.y);
                }
                pointLast = pointCurr;
            }
	}
        graphics.setColor(colorZ);
	for( int m = 0; m < m_oTraceZy.size(); m ++ )
	{
            Point           pointLast = null;
            CTracePoints    pTrace = (CTracePoints)m_oTraceZy.get(m);
            int             nCount = pTrace.m_GetCount();
            for( int n = 0; n < nCount; n ++ )
            {
                Point   pointCurr = pTrace.m_GetAt(n);
                if( pointLast != null && pointCurr != null )
                {//当前点与前一点不连续
                    graphics.drawLine(pointLast.x,pointLast.y,pointCurr.x,pointCurr.y);
                }
                pointLast = pointCurr;
            }
	}
        /*
	for( int n = 0; n < m_oTraceZy.size(); n ++ )
	{
            Point		pointCurr = null;
            CTracePoints	pTrace = (CTracePoints)m_oTraceZy.get(n);
            graphics.drawPolyline(pTrace.m_GetXs(),pTrace.m_GetYs(),pTrace.m_GetCount());
        }
        */
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -