ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-缩放和缩放后屏幕坐标和图像坐标之间的转换

2019-11-18 13:04:45  阅读:581  来源: 互联网

标签:zooming translation scaling java


我有一堂课,负责管理从屏幕坐标到图像坐标的转换.
但是,我有一个“一个错误”.

以下给出了318、198,而不是319、199:

@Test
public void test6rightScreenCornerToImageCoOrdAfterZoomingAndScaling() {
    PointTranslation pointTranslation = new PointTranslation();
    pointTranslation.setOriginalSize(0, 0, 320, 200); // original image
    pointTranslation.zoomIn(9, 9, 310, 190); // zoomed image starting at 9,9
    pointTranslation.scale(0, 0, 800, 800);

    Point translatedPoint = pointTranslation.transformPoint(799,799);
    System.out.println(testName.getMethodName() + " : " + translatedPoint.toString());
    assertTrue(translatedPoint.x == 319);
    assertTrue(translatedPoint.y == 199);
}

================================================= ===========

完整清单:

package gtx;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.gdip.PointF;
import org.eclipse.swt.internal.gdip.RectF;

@SuppressWarnings("restriction")
public class PointTranslation {
RectF originalSize = null;
RectF currentSize = null;
RectF scaledSize = null;

public void setOriginalSize(int originX, int originY, int width, int height) {
    originalSize = getRectangle(originX, originY, width, height);
    currentSize = originalSize;
}

public void zoomIn(int originX, int originY, int width, int height) {
    // System.out.println("addTranslation: " + originX + " " + originY + " "
    // + width + " " + height);
    currentSize = getRectangle(originX, originY, width, height);
}

public void scale(int originX, int originY, int width, int height) {
    // System.out.println("addTranslation: " + originX + " " + originY + " "
    // + width + " " + height);
    scaledSize = getRectangle(originX, originY, width, height);
}

public boolean isPointWithinBounds(Point point) {
    return isPointWithinBounds(point.x, point.y);
}

public boolean isPointWithinBounds(int xPos, int yPos) {
    boolean ret = false;

    if (scaledSize != null) {
        RectF sourceRec = scaledSize;
        int xBounds = (int) (sourceRec.Width + sourceRec.X);
        int yBounds = (int) (sourceRec.Height + sourceRec.Y);
        ret = (xPos < xBounds) && (yPos < yBounds) && (xPos > sourceRec.X) && (yPos > sourceRec.Y);
    }

    return ret;
}

public Point transformPoint(Point point) {
    return transformPoint(point.x, point.y);
}

public Point transformPoint(int xPos, int yPos) {
    Point sourcePoint = new Point((int) xPos, (int) yPos);
    Point retPoint = sourcePoint;

    if (this.scaledSize != null) {
        retPoint = transformPoint(this.scaledSize, this.currentSize, sourcePoint);
    }
    return retPoint;
}

/*
 * Rectangle 1 has (x1, y1) origin and (w1, h1) for width and height, and
 * Rectangle 2 has (x2, y2) origin and (w2, h2) for width and height, then
 * 
 * Given point (x, y) in terms of Rectangle 1 co-ords, to convert it to
 * Rectangle 2 co-ords: xNew = ((x-x1)/w1)*w2 + x2; yNew = ((y-y1)/h1)*h2 +
 * y2;
 */
private Point transformPoint(RectF source, RectF destination, Point intPoint) {
    PointF point = new PointF();
    point.X = intPoint.x;
    point.Y = intPoint.y;
    return transformPoint(source, destination, point);
}

private Point transformPoint(RectF source, RectF destination, PointF point) {
    return new Point((int) ((((point.X - source.X) / source.Width) * destination.Width + destination.X)),
            (int) ((((point.Y - source.Y) / source.Height) * destination.Height + destination.Y)));
}

private RectF getRectangle(int x, int y, int width, int height) {
    RectF rect = new RectF();
    rect.X = x;
    rect.Y = y;
    rect.Height = height;
    rect.Width = width;
    return rect;
}

private PointF getPoint(int x, int y) {
    PointF retPoint = new PointF();
    retPoint.X = x;
    retPoint.Y = y;
    return retPoint;
}

public void reset() {
    this.originalSize = null;
    this.currentSize = null;
    this.scaledSize = null;
}
}

更新:

我的问题肯定是四舍五入.奇怪的是,对于某些测试用例,我需要向上舍入以获得正确的点,有时我需要向上舍入.我缺少诸如缩放因子之类的东西.任何建议如何正确地在两个矩形之间进行平移?

更新2:

我尝试了以下方法,但仍然不满意:

    private Point transformPoint(RectF source, RectF destination, PointF point) {   
        float xPercent = normalize(point.X,source.X,source.Width);
        float destX = xPercent*(Math.abs(destination.Width - destination.X)) + destination.X;

        float yPercent = normalize(point.Y,source.Y,source.Height);
        float destY = yPercent*(Math.abs(destination.Height - destination.Y)) + destination.Y;

        System.out.println("Float x,y: " + destX + ", " + destY);
        System.out.println("Ceil Float x,y: " + Math.floor(destX) + ", " + Math.floor(destY) );

        return new Point((int)Math.floor(destX), (int)Math.floor(destY));
    }

    private float normalize(float value, float min, float max) {
        return Math.abs((value - min) / (max - min));
    }

解决方法:

在运行测试用例并逐步执行代码时,我的调试显示了以下替代内容…

transformPoint(RectF source, RectF destination, PointF point) {
    return new Point (
        (int) (((( 799 - 0 ) / 800 ) * 310 ) + 9 )),
        (int) (((( 799 - 0 ) / 800 ) * 190 ) + 9 ))
    );
}

该方程式的前半部分返回318.6125.等式的后半部分返回198.7625.

你需要

>修改方程式,以便将int转换截断为
期望值(例如末尾为1)
>在转换为int之前四舍五入
>与结果共存

正如Mathew指出的那样,连续多次翻译使问题变形和放大,就像平均平均值一样.

标签:zooming,translation,scaling,java
来源: https://codeday.me/bug/20191118/2028145.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有