正如标题所说,如果我缩放形状shape.scale(0.5)
,为什么它不起作用?当应用scale()时,PShapeSVG contains()函数不起作用
即使我做shape(0,0,200,200)
也不起作用,这意味着我不是在原始尺寸中绘制形状。这是一个错误还是我错过了什么?
正如标题所说,如果我缩放形状shape.scale(0.5)
,为什么它不起作用?当应用scale()时,PShapeSVG contains()函数不起作用
即使我做shape(0,0,200,200)
也不起作用,这意味着我不是在原始尺寸中绘制形状。这是一个错误还是我错过了什么?
这是一种错误,虽然我不确定有多严重。从您的测试中发现,当使用转换(平移/旋转/缩放)时,方法不起作用。
我有两个稍微哈克的解决方法:
第一个解决方案听起来像是一点工作和没有意义的数据重复,当谈到处理旋转时并没有提到恼人的问题,而且还出现了“堆积”/复杂变换的错误。
第二种解决方法看起来有点冒险,因为contains()应该刚刚工作,但它使用了Processing类,所以它并没有那么糟糕。它的工作原理是这样的:
SVG的来自示例>基本>形状> GetChild。您可以打开草图文件夹(按Ctrl + K/CMD + K)获得 “USA-wikipedia.svg” 如果你要测试的代码是:
import processing.opengl.*;
PShape ohio;
PMatrix2D coordSysSvgInv;//svg coordinate system inversed
void setup() {
size(1200, 480,OPENGL);//the catch is, even though we use PMatrix2D, PShape's applyMatrix() only seems to work with the P3D or OpenGL renderer
PShape usa = loadShape("usa-wikipedia.svg");
ohio = (PShape)usa.getChild("OH");
PMatrix2D transform = new PMatrix2D(); //apply transforms(position,rotation,scale) to this matrix
transform.scale(2); //be aware that the order of operation matters!
transform.translate(-800,-300); //this matrix can be used to convert from screen coordinats to SVG coordinates
coordSysSvgInv = transform.get(); //clone the svg to screen transformation matrix
coordSysSvgInv.invert(); //simply invert it to get the screen to svg
ohio.applyMatrix(transform); //apply this transformation matrix to the SVG
}
void draw() {
//update
PVector mouseInSVG = screenToSVG(mouseX,mouseY);
boolean isOver = ohio.contains(mouseInSVG.x,mouseInSVG.y);
//draw
background(255);
ohio.disableStyle();
fill(isOver ? color(0,192,0) : color(255,127,0));
shape(ohio);
}
PVector screenToSVG(float x,float y){
PVector result = new PVector();//create a new PVector to store transformed vector
coordSysSvgInv.mult(new PVector(x,y),result);//transform PVector by multiplying it to the inverse svg coord. sys.
return result;
}
我已经注意到,applyMatrix()方法只与P3D
和OPENGL
渲染甚至以为我passing a PMatrix2D情况下工作的,否则会出现这样的警告:
applyMatrix() with x, y, and z coordinates can only be used with a renderer that supports 3D, such as P3D or OPENGL. Use a version without a z-coordinate instead.
的“清洁”选项来修改PShape类的contains()方法,然后重新编译处理的核心。 jar并使用更新的jar。如果这是一次性的小型项目,我不知道是否值得这么麻烦,但从重新编译/更新core.jar开始,稍微有些混乱的代码可能会更快。
我的解决方案适合您吗?我也发布了这个问题(http://code.google.com/p/processing/issues/detail?id=1242),以防万一。 –