本文整理匯總了Java中com.jme3.math.Vector2f.distanceSquared方法的典型用法代碼示例。如果您正苦於以下問題:Java Vector2f.distanceSquared方法的具體用法?Java Vector2f.distanceSquared怎麽用?Java Vector2f.distanceSquared使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.jme3.math.Vector2f
的用法示例。
在下文中一共展示了Vector2f.distanceSquared方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: minimum_distance
import com.jme3.math.Vector2f; //導入方法依賴的package包/類
float minimum_distance(Vector2f v, Vector2f w, Vector2f p) {
// Return minimum distance between line segment vw and point p
float l2 = v.distanceSquared(w); // i.e. |w-v|^2 - avoid a sqrt
if (l2 == 0.0) {
return p.distance(v); // v == w case
}
// Consider the line extending the segment, parameterized as v + t (w - v).
// We find projection of point p onto the line.
// It falls where t = [(p-v) . (w-v)] / |w-v|^2
// We clamp t from [0,1] to handle points outside the segment vw.
float t = Math.max(0, Math.min(1, (p.subtract(v)).dot(w.subtract(v)) / l2));
Vector2f projection = v.add(w.subtract(v).multLocal(t)); // Projection falls on the segment
return p.distance(projection);
}
示例2: doPaintAction
import com.jme3.math.Vector2f; //導入方法依賴的package包/類
/**
* Goes through each pixel in the image. At each pixel it looks to see if the UV mouse coordinate is within the
* of the brush. If it is in the brush radius, it gets the existing color from that pixel so it can add/subtract to/from it.
* Essentially it does a radius check and adds in a fade value. It does this to the color value returned by the
* first pixel color query.
* Next it sets the color of that pixel. If it was within the radius, the color will change. If it was outside
* the radius, then nothing will change, the color will be the same; but it will set it nonetheless. Not efficient.
* <p>
* If the mouse is being dragged with the button down, then the dragged value should be set to true. This will reduce
* the intensity of the brush to 10% of what it should be per spray. Otherwise it goes to 100% opacity within a few pixels.
* This makes it work a little more realistically.
*
* @param colorFunction the color function.
* @param image to manipulate
* @param uv the world x,z coordinate
* @param radius in percentage so it can be translated to the image dimensions
* @param erase true if the tool should remove the paint instead of add it
* @param fadeFalloff the percentage of the radius when the paint begins to start fading
*/
private void doPaintAction(@NotNull final ObjectFloatObjectConsumer<ColorRGBA, Boolean> colorFunction,
@NotNull final Image image, @NotNull final Vector2f uv, @NotNull final Vector2f temp,
@NotNull final ColorRGBA color, final float radius, final boolean erase,
final float fadeFalloff) {
final ByteBuffer buffer = image.getData(0);
final int width = image.getWidth();
final float height = image.getHeight();
// convert percents to pixels to limit how much we iterate
final int minX = (int) Math.max(0, (uv.getX() * width - radius * width));
final int maxX = (int) Math.min(width, (uv.getX() * width + radius * width));
final int minY = (int) Math.max(0, (uv.getY() * height - radius * height));
final int maxY = (int) Math.min(height, (uv.getY() * height + radius * height));
final float radiusSquared = radius * radius;
// go through each pixel, in the radius of the tool, in the image
for (int y = minY; y < maxY; y++) {
for (int x = minX; x < maxX; x++) {
// gets the position in percentage so it can compare with the mouse UV coordinate
temp.set((float) x / width, (float) y / height);
float dist = temp.distanceSquared(uv);
// if the pixel is within the distance of the radius, set a color (distance times intensity)
if (dist < radiusSquared) {
final int position = (y * width + x) * 4;
if (position > buffer.capacity() - 1 || position < 0) {
continue;
}
// gets the color at that location (false means don't write to the buffer)
manipulatePixel(image, buffer, color, position, false);
// calculate the fade falloff intensity
final float intensity = (1.0f - (dist / radiusSquared)) * fadeFalloff;
colorFunction.accept(color, intensity, erase);
color.clamp();
change(position, color);
// set the new color
manipulatePixel(image, buffer, color, position, true);
}
}
}
image.getData(0).rewind();
}