javafx Shape3D with border - javafx-8

I'm making application with huge mass of 3D shapes and I need them fully transparent and with border. I tried find any way to apply border to Shape3D, specifically to Box and Sphere but I can't find anything. So my questions are:
Is there any way how to add border to Shape3D?
If yes, how to do it?

No, there is no option to add borders to 3d shapes, but you can use very thin cyllinders instead (only works for boxes though):
public void createBoxLines(double contW, double contH, double contD, double x, double y, double z) {
//You call this method to create a box with a size and location you put in
//This method calls the createLine method for all the sides of your rectangle
Point3D p1 = new Point3D(x, y, z);
Point3D p2 = new Point3D(contW + x, y, z);
Point3D p3 = new Point3D(x, contH + y, z);
Point3D p4 = new Point3D(contW + x, contH + y, z);
createLine(p1, p2);
createLine(p1, p3);
createLine(p3, p4);
createLine(p2, p4);
Point3D p5 = new Point3D(x, y, contD + z);
Point3D p6 = new Point3D(contW + x, y, contD + z);
Point3D p7 = new Point3D(x, contH + y, contD + z);
Point3D p8 = new Point3D(contW + x, contH + y, contD + z);
createLine(p5, p6);
createLine(p5, p7);
createLine(p7, p8);
createLine(p6, p8);
createLine(p1, p5);
createLine(p2, p6);
createLine(p3, p7);
createLine(p4, p8);
}
double strokewidth = 1;
public void createLine(Point3D origin, Point3D target) {
//creates a line from one point3d to another
Point3D yAxis = new Point3D(0, 1, 0);
Point3D diff = target.subtract(origin);
double height = diff.magnitude();
Point3D mid = target.midpoint(origin);
Translate moveToMidpoint = new Translate(mid.getX(), mid.getY(), mid.getZ());
Point3D axisOfRotation = diff.crossProduct(yAxis);
double angle = Math.acos(diff.normalize().dotProduct(yAxis));
Rotate rotateAroundCenter = new Rotate(-Math.toDegrees(angle), axisOfRotation);
Cylinder line = new Cylinder(strokewidth, height);
line.getTransforms().addAll(moveToMidpoint, rotateAroundCenter);
myGroup.getChildren().add(line);
}
The createLine method can be used seperately to make lines between different points.
I cant provide many comments for that method, because I basically copied it from some blog. Although I'm having a hard time finding that blog again.

Thanks Alex Quilliam thanks for the code i was able to improve my program.
https://i.imgur.com/HY2x9vF.png
Cylinder line = new Cylinder(strokewidth, height);
↓
Box line = new Box(strokewidth, height, strokewidth);
JavaFX_3D_Cube_Outline_Test

Related

How to get the points (coordinates) on 2D Line?

When I plot point1(p1) and point2(p2), the line between p1 and p2 is drawn. I wanna know a set of the points making the line.
For example, I wanna get x, y coordinates (as array type: x[], y[]). Is there any algorithms or code?
Here's what I have come up with:
It is fair to say that we need to use the slope formula, y = m*x + b to find the slope so we can plot our points along that line. We need the following:
(x1, y1)
(x2, y2)
to find the following:
m = (y2 - y1) / (x2 - x1)
b = y1 - (m * x1)
minX = min(x1, x2) used for limiting our lower bound
maxX = max(x1, x2) used for limiting our upper bound
Now that everything is set, we can plot our line pixel by pixel and obtain all (x,y) coordinates we need. The logic is simple:
let x loop from minX to maxX and plug it in y = m*x + b (we already have all the variables except y). Then, store the (x,y) pair.
I have used Java for coding this logically and visually. Also, I used LinkedList instead of arrays (because I we can't know the number of points we will obtain).
I have also drawn what Java would draw (in blue) and my approach (in red). They are almost perfectly the exact output and coordinates. The image below is zoomed 5x the original size.
Note! The above explanation is what you would use if the line is not vertical (because the slope would be undefined, division by zero). If it is, then you will plug y (instead of x) values and find the x (instead of y) value from the following formula x = (y - b) / m (instead of y = m*x + b). Though, the code takes care of vertical lines.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.LinkedList;
import javax.swing.JFrame;
public class LineDrawing extends Canvas {
int x1 = 5;
int y1 = 10;
int x2 = 105;
int y2 = 100;
double m = ((double) (y2 - y1)) / ((double) (x2 - x1));//slope
double b = y1 - (m * ((double) x1));//vertical shift
//Takes care of the domain we will loop between.
//min and max will be assigned minX and maxX if the line is not vertical.
//minY and maxY are assigned to min and max otherwise.
int minX = Math.min(x1, x2);//minimum x value we should consider
int maxX = Math.max(x1, x2);//maximum x value we should consider
int minY = Math.min(y1, y2);//minimum y value we should consider
int maxY = Math.max(y1, y2);//maximum y value we should consider
int min = 0;
int max = 0;
boolean plugX = true;//if true, the line is not vertical.
LinkedList<Point> points = new LinkedList<>();//Store all points here
public LineDrawing() {
if (x1 == x2) {//plug the y value instead the x, this is a vertical line.
plugX = false;
min = minY;
max = maxY;
} else {//dont change and plug x values.
min = minX;
max = maxX;
}
}
#Override
public void paint(Graphics g) {
super.paint(g);
//Draw the line, using default java drawLine in blue.
g.setColor(Color.BLUE);
g.drawLine(x1, y1, x2, y2);
//change the color to red, it will draw our verison.
g.setColor(Color.RED);
//Draw the points, point by point on screen.
//Plug m, x, and b in the formula y = m*x + b
//to obtain the y value.
//OR
//Plug m, y, and b in the formula x = (y - b) / m
//to obtain the x value if vertical line.
//Then plot (x,y) coordinate on screen and add the point to our linkedList.
for (int i = min; i <= max; i++) {
int obtained = 0;
if (plugX) {//not a vertical line
obtained = (int) Math.round((m * i + b));
System.out.println("x = " + i + " , y = " + obtained);
points.add(new Point(i, obtained));
//Uncomment to see the full blue line.
g.drawLine(i, obtained, i, obtained);
} else {//vertical line
obtained = (int) Math.round((double) (i - b) / (double) m);
System.out.println("x = " + x1 + " , y = " + i);
g.drawLine(x1, i, x1, i);//Uncomment to see the full blue line.
points.add(new Point(x1, i));
}
}
//Print out the number of points as well as the coordinates themselves.
System.out.println("Total points: " + points.size());
for (int i = 0; i < points.size(); i++) {
System.out.println(i + " ( " + points.get(i).x
+ ", " + points.get(i).y + " )");
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(120, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new LineDrawing());
frame.setVisible(true);
}
}

How to add a shading pattern to a custom shape

I have drawn an equilateral triangle as follows using iText
canvas.setColorStroke(BaseColor.BLACK);
int x = start.getX();
int y = start.getY();
canvas.moveTo(x,y);
canvas.lineTo(x + side,y);
canvas.lineTo(x + (side/2), (float)(y+(side*Math.sin(convertToRadian(60)))));
canvas.closePathStroke();
I wish to multi color gradient in this shape i.e. fill it with shading comprising of BaseColor.PINK and BaseColor.BLUE. I just can't find a way to do this with iText ?
I've created an example called ShadedFill that fills the triangle you are drawing using a shading pattern that goes from pink to blue as show in the shaded_fill.pdf:
PdfContentByte canvas = writer.getDirectContent();
float x = 36;
float y = 740;
float side = 70;
PdfShading axial = PdfShading.simpleAxial(writer, x, y,
x + side, y, BaseColor.PINK, BaseColor.BLUE);
PdfShadingPattern shading = new PdfShadingPattern(axial);
canvas.setShadingFill(shading);
canvas.moveTo(x,y);
canvas.lineTo(x + side, y);
canvas.lineTo(x + (side / 2), (float)(y + (side * Math.sin(Math.PI / 3))));
canvas.closePathFillStroke();
As you can see, you need to create a PdfShading object. I created an axial shading that varies from pink to blue from the coordinate (x, y) to the coordinate (x + side, y). With this axial shading, you can create a PdfShadingPattern that can be used as a parameter of the setShadingFill() method to set the fill color for the canvas.
See ShadedFill for the full source code.

raised border SWT/JFace

i can't seem to find online help on how to add different type of borders in Eclipse RCP. I know Swing has BevelBorder which can be achieved using BorderFactory. Any swt equivalence?
Try this styles: SWT.SHADOW_IN, SWT.SHADOW_OUT, SWT.SHADOW_ETCHED_IN, SWT.SHADOW_ETCHED_OUT
Please use this method for Bevel Border
private void drawBorders(GC gc, int x, int y, int w, int h) {
final Display disp = getDisplay();
final Color topleft = disp.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW);
final Color bottomright = disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
if (topleft != null && bottomright != null) {
gc.setLineWidth(1);
gc.setForeground(bottomright);
gc.drawLine(x + w, y, x + w, y + h);
gc.drawLine(x, y + h, x + w, y + h);
gc.setForeground(topleft);
gc.drawLine(x, y, x + w - 1, y);
gc.drawLine(x, y, x, y + h - 1);
}
}

iPhone opengl es: rotate a matrix about a point

I am doing some work with opengl es on the iPhone and I am stuck at a particular point. All the code samples on the internet show you how a matrix can be rotated about the x axis, y axis or the z axis but no one talks about how a matrix can be rotated about an arbitrary point?
I am using open gl es 2.0. Any help would be appreciated.
Regards,
It sounds like you're asking how to construct a matrix that rotates around one of those axes, but at a different point. The way you do that is to first translate to that point, and then apply the rotation for the axis you want. The order of multiplication of matrixes depends on whether you think of it as the axes moving or the geometry.
If you also want to be able to do a rotation of arbitrary x, y, z angle at the same time, you can use the matrix discussed in this article:
static inline void Matrix3DSetRotationByRadians(Matrix3D matrix, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
{
GLfloat mag = sqrtf((x*x) + (y*y) + (z*z));
if (mag == 0.0)
{
x = 1.0;
y = 0.0;
z = 0.0;
}
else if (mag != 1.0)
{
x /= mag;
y /= mag;
z /= mag;
}
GLfloat c = cosf(angle);
GLfloat s = fastSinf(angle);
matrix[3] = matrix[7] = matrix[11] = matrix[12] = matrix[13] = matrix[14] = 0.0;
matrix[15] = 1.0;
matrix[0] = (x*x)*(1-c) + c;
matrix[1] = (y*x)*(1-c) + (z*s);
matrix[2] = (x*z)*(1-c) - (y*s);
matrix[4] = (x*y)*(1-c)-(z*s);
matrix[5] = (y*y)*(1-c)+c;
matrix[6] = (y*z)*(1-c)+(x*s);
matrix[8] = (x*z)*(1-c)+(y*s);
matrix[9] = (y*z)*(1-c)-(x*s);
matrix[10] = (z*z)*(1-c)+c;
}

Finding min/max of quadratic bezier with CoreGraphics

I am using CoreGraphics to draw a quadratic bezier but want to computer the min/max value of the curve. I am not from a mathematical background so this has become a bit troublesome. Does anyone have any articles or ideas about how to solve this?
For a quadratic Bezier, this is actually quite simple.
Define your three control points as P0 = (x0,y0), P1 = (x1,y1) and P2 = (x2,y2). To find the extrema in x, solve this equation:
t = (x0 - x1) / (x0 - 2*x1 + x2)
If 0 <= t <= 1, then evaluate your curve at t and store the location as Px. Do the same thing for y:
t = (y0 - y1) / (y0 - 2*y1 + y2)
Again, if 0 <= t <= 1, evaluate your curve at t and store the location as Py. Finally, find the axis-aligned bounding box containing P0, P2, Px (if found) and Py (if found). This bounding box will also tightly bound your 2D quadratic Bezier curve.
Calculus gives the standard box of tricks for finding the min/max of continuous, differentiable curves.
Here is a sample discussion.
I have made a representation of this in javascript:
Jsfiddle link
function P(x,y){this.x = x;this.y = y; }
function pointOnCurve(P1,P2,P3,t){
if(t<=0 || 1<=t || isNaN(t))return false;
var c1 = new P(P1.x+(P2.x-P1.x)*t,P1.y+(P2.y-P1.y)*t);
var c2 = new P(P2.x+(P3.x-P2.x)*t,P2.y+(P3.y-P2.y)*t);
return new P(c1.x+(c2.x-c1.x)*t,c1.y+(c2.y-c1.y)*t);
}
function getQCurveBounds(ax, ay, bx, by, cx, cy){
var P1 = new P(ax,ay);
var P2 = new P(bx,by);
var P3 = new P(cx,cy);
var tx = (P1.x - P2.x) / (P1.x - 2*P2.x + P3.x);
var ty = (P1.y - P2.y) / (P1.y - 2*P2.y + P3.y);
var Ex = pointOnCurve(P1,P2,P3,tx);
var xMin = Ex?Math.min(P1.x,P3.x,Ex.x):Math.min(P1.x,P3.x);
var xMax = Ex?Math.max(P1.x,P3.x,Ex.x):Math.max(P1.x,P3.x);
var Ey = pointOnCurve(P1,P2,P3,ty);
var yMin = Ey?Math.min(P1.y,P3.y,Ey.y):Math.min(P1.y,P3.y);
var yMax = Ey?Math.max(P1.y,P3.y,Ey.y):Math.max(P1.y,P3.y);
return {x:xMin, y:yMin, width:xMax-xMin, height:yMax-yMin};
}