JavaFX line fill color - line

I'm trying to create a line with a different fill and stroke color, something like this:
I have tried the following:
Line line = new Line(0,0,100,100);
line.setFill(Color.RED);
line.setStroke(Color.BLACK);
line.setStrokeWidth(10);
but this gives me just a black line.
Is what I'm trying to do possible with a simple Line or do I have to use another Shape? (I would prefer using a line because I have to frequently call the setStartX, setStartY, ... methods)

If you check this question, you'll see that you can only use setStroke. Also, a possible approach to generate the same styling is proposed by using a linear gradiant.
This will work (adjust stops at your convenience for more or less black width):
Line line = new Line(0,0,100,0);
line.setStrokeWidth(10);
line.setStroke(new LinearGradient(0d, -5d, 0d, 5d, false,
CycleMethod.NO_CYCLE, new Stop(0,Color.BLACK),
new Stop(0.199,Color.BLACK),
new Stop(0.2,Color.RED),
new Stop(0.799,Color.RED),
new Stop(0.8,Color.BLACK)));
Note also that since the gradient is not proportional, you need to use rotation to generate not horizontal lines.

The answer of José Pereda is more elegant but I couldn't get the math right to create diagonal lines so as a workaround I simply created two lines, each with a different color:
Line stroke = new Line(0, 0, 100, 100);
Line fill = new Line(0, 0, 100, 100);
stroke.setStrokeWidth(10);
fill.setStrokeWidth(8);
stroke.setStroke(Color.BLACK);
fill.setStroke(Color.RED);
pane.addAll(stroke, fill);
No math required and I can keep on using the setStartX,setStartY, ... methods of the lines although I now have double the amount of lines.

Related

createjs combine 2 shapes in a mask

I have a symbol S1 with two shapes (lets say sh0 and sh1). On the stage I have an instance of another symbol mc. At run time, I will create an instance mc1 of the symbol S1. Using createjs, how can I use mc1 as a mask for mc?
I assume when you say "symbol", you mean a graphic or MovieClip in Adobe Animate. Unfortunately, you can only use a CreateJS "Shape" as a mask directly. There are a few options:
Combine the shapes into one yourself.
Combine the instructions. This is a bit dirty, but you could in theory concat the graphic instructions from one shape into another. I suspect this would have issues if the Shape instances have different x/y positions.
symbol.shape1.graphics._instructions.concat(symbol.shape2.graphics._instructions);
Cache the symbol as use it as a Mask with AlphaMaskFilter. The example in the documents should get you what you want.
var box = yourSymbol;
box.cache(0, 0, 100, 100);
var bmp = new createjs.Bitmap("path/to/image.jpg");
bmp.filters = [
new createjs.AlphaMaskFilter(box.cacheCanvas)
];
bmp.cache(0, 0, 100, 100);
The 3rd is probably your best option, but it is limiting and can be performance intensive due to the use of the filter (especially if content changes and you have to update it constantly).
Feel free to post more details on what you are working with in order to get a better recommendation.
Cheers.

VTK cutter output

I am seeking a solution of connecting all the lines that have the same slope and share a common point. For example, after I load a STL file and cut it using a plane, the cutter output includes the points defining the contour. Connecting them one by one forms a (or multiple) polyline. However, some lines can be merged when their slopes are the same and they share a common point. E.g., [[0,0,0],[0,0,1]] and [[0,0,1],[0,0,2]] can be represented by one single line [[0,0,0],[0,0,2]].
I wrote a function that can analyse all the lines and connect them if they can be merged. But when the number of lines are huge, this process is slow. I am thinking in the VTK pipeline, is there a way to do the line merging?
Cheers!
plane = vtk.vtkPlane()
plane.SetOrigin([0,0,5])
plane.SetNormal([0,0,1])
cutter = vtk.vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInput(triangleFilter.GetOutput())
cutter.Update()
cutStrips = vtk.vtkStripper()
cutStrips.SetInputConnection(cutter.GetOutputPort())
cutStrips.Update()
cleanDataFilter = vtk.vtkCleanPolyData()
cleanDataFilter.AddInput(cutStrips.GetOutput())
cleanDataFilter.Update()
cleanData = cleanDataFilter.GetOutput()
print cleanData.GetPoint(0)
print cleanData.GetPoint(1)
print cleanData.GetPoint(2)
print cleanData.GetPoint(3)
print cleanData.GetPoint(4)
The output is:
(0.0, 0.0, 5.0)
(5.0, 0.0, 5.0)
(10.0, 0.0, 5.0)
(10.0, 5.0, 5.0)
(10.0, 10.0, 5.0)
Connect the above points one by one will form a polyline representing the cut result. As we can see, the line [point0, point1] and [point1, point2] can be merged.
Below is the code for merging the lines:
Assume that the LINES are represented by list: [[(p0),(p1)],[(p1),(p2)],[(p2),(p3)],...]
appended = 0
CurrentLine = LINES[0]
CurrentConnectedLine = CurrentLine
tempLineCollection = LINES[1:len(LINES)]
while True:
for HL in tempLineCollection:
QCoreApplication.processEvents()
if checkParallelAndConnect(CurrentConnectedLine, HL):
appended = 1
LINES.remove(HL)
CurrentConnectedLine = ConnectLines(CurrentConnectedLine, HL)
processedPool.append(CurrentConnectedLine)
if len(tempLineCollection) == 1:
processedPool.append(tempLineCollection[0])
LINES.remove(CurrentLine)
if len(LINES) >= 2:
CurrentLine = LINES[0]
CurrentConnectedLine = CurrentLine
tempLineCollection = LINES[1:len(LINES)]
appended = 0
else:
break
Solution:
I figured out a way of further accelerating this process using some vtk data structure. I found out that a polyline line will be stored in a cell, which can be checked by using GetCellType(). Since the point order for a polyline is sorted already, We do not need to search globally which lines are colinear with the current one. For each point on the polyline, I just need to check the point[i-1], point[i], point[i+1]. And if they are colinear, the end of the line will be updated to the next point. This process continues until the end of the polyline is reached. The speed increases by a huge amount compared with the global search approach.
Not sure if it is the main source of slowness (depends on how many positive hits on the colinearity you have), but removing items from a vector is costly (O(n)), since it requires reorganizing the rest of the vector, you should avoid it. But even without hits on colinearity, the LINES.remove(CurrentLine) call is surely slowing things down and there isn't really any need for it - just leave the vector untouched, write the final results to a new vector (processedPool) and get rid of the LINES vector in the end. You can modify your algorithm by making a bool array (vector), initialized at "false" for each item, then when you remove a line, you don't actually remove it, but only mark it as "true" and you skip all lines for which you have "true", i.e. something like this (I don't speak python so the syntax is not accurate):
wasRemoved = bool vector of the size of LINES initialized at false for each entry
for CurrentLineIndex = 0; CurrentLineIndex < sizeof(LINES); CurrentLineIndex++
if (wasRemoved[CurrentLineIndex])
continue // skip a segment that was already removed
CurrentConnectedLine = LINES[CurrentLineIndex]
for HLIndex = CurrentLineIndex + 1; HLIndex < sizeof(LINES); HLIndex++:
if (wasRemoved[HLIndex])
continue;
HL = LINES[HLIndex]
QCoreApplication.processEvents()
if checkParallelAndConnect(CurrentConnectedLine, HL):
wasRemoved[HLIndex] = true
CurrentConnectedLine = ConnectLines(CurrentConnectedLine, HL)
processedPool.append(CurrentConnectedLine)
wasRemoved[CurrentLineIndex] = true // this is technically not needed since you won't go back in the vector anyway
LINES = processedPool
BTW, the really correct data structure for LINES to use for that kind of algorithm would be a linked list, since then you would have O(1) complexity for removal and you wouldn't need the boolean array. But a quick googling showed that that's not how lists are implemented in Python, also don't know if it would not interfere with other parts of your program. Alternatively, using a set might make it faster (though I would expect times similar to my "bool array" solution), see python 2.7 set and list remove time complexity
If this does not do the trick, I suggest you measure times of individual parts of the program to find the bottleneck.

Set Table Column width problems in iText7

When I want to create table in PDF, I can use the following two ways:
But the first one failed.
method 1:
float[] columnWidths = {20, 30, 50};
Table table = new Table(columnWidths);
It's failed to control columnWidths
method 2:
UnitValue[] unitValue = new UnitValue[]{
UnitValue.createPercentValue((float) 20),
UnitValue.createPercentValue((float) 30),
UnitValue.createPercentValue((float) 50)};
Table table = new Table(columnWidths);
It's success!
Why does this happen?
Yes, there is no way to scale column widths in iText 7.0.2, as Vernon said.
If you want to use directly 20, 30, and 50 points for columns despite min widths you shall use this construction:
Table table = new Table(new float[] {20, 30, 50}) // in points
.setWidth(100) //100 pt
.setFixedLayout();
If you set fixed layout you must set width as well, it is required for fixed layout.
The Release Notes for iText Core 7.0.2 include the bullet point "improved auto layout and fixed layout for tables, scaled column widths have been removed". If you need the old functionality, you'll need to go back to 7.0.1!
The online API docn for the Table constructor says Note, since 7.0.2 in case auto layout column width values less than min width will be ignored. It's not great English, but I believe it's trying to say that the supplied values will not be scaled up so the columns fill the table, but instead will be used as-is, in which case they're almost certain to be too small, so will be ignored. Overall, this change makes sense, since treating a handful of floats as a ratio is a bit arbitrary for a good API. Using values with units is much more sensible, and your example using proper percentages is a good one.

PsychoPy: How to change number of elements in DotStim or ElementArrayStim?

Say I generated a dots = psychopy.visual.DotStim. Is it possible to change the number of dots later? dots.nDots = 5 leads to an error on the next dots.draw() because the underlying matrices don't match:
Traceback (most recent call last):
File "/home/jonas/Documents/projects/work pggcs/experiment/dots.py", line 32, in <module>
dots_right.draw()
File "/usr/lib/python2.7/dist-packages/psychopy/visual/dot.py", line 279, in draw
self._update_dotsXY()
File "/usr/lib/python2.7/dist-packages/psychopy/visual/dot.py", line 362, in _update_dotsXY
self._verticesBase[:,0] += self.speed*numpy.reshape(numpy.cos(self._dotsDir),(self.nDots,))
File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 218, in reshape
return reshape(newshape, order=order)
ValueError: total size of new array must be unchanged
The same is true of psychopy.visual.ElementArrayStim for which setting stim.nElements = 5 similarly results in an error on the next draw.
A solution is of course to instantiate a whole new DotStim or ElementArrayStim every time the number of dots/elements should change but this seems too heavy.
It can be fixed for the DotStim:
dots.nDots = 5
dots._dotsDir = [0]*dots.nDots
dots. _verticesBase = dots._newDotsXY(dots.nDots)
This set movement of all dots to 0 but you can change that value to whatever you like or specify for individual dots. This is a hack which will likely break if you modify other aspects of the DotStim.
I haven't figured out a solution for ElementArrayStim.
Yes, it would be nice to be able to do this but I haven't gotten around to it. The code that has to be run when the nDots/nElements changes is pretty close to starting from scratch with a new stimulus.init so adding this in 'correctly' probably means some refactoring (move a lot of the init code into setNDots() and then call it from init).
There's an additional potential issue which is that the elements might have changed (e.g. the user has set the orientations) and then updates the number of elements. Which ones do we remove? And what orientation do we give those that we add? (This is less of an issue for DotStim though)
Basically, the issue is a little thorny and hasn't ben a priority for me.

axis orient / position with dimple.js

Is there a way to explicitly position/orient an axis using dimple.js? I know that the first x-axis is added to the bottom and the second is added to the top, so I can get the desired outcome like this:
var xAxis = myChart.addMeasureAxis("x", "dur");
xAxis.hidden = true;
xAxis = myChart.addMeasureAxis("x", "dur");
...but that seems a little hacked.
I don't think there's any way around that. Here's the relevant code : https://github.com/PMSI-AlignAlytics/dimple/blob/master/src/objects/chart/methods/draw.js#L123
Because firstX is local to the draw function there isn't a way you could override it within the context of the loop.
You could possibly duplicate the logic used to position the top axis : https://github.com/PMSI-AlignAlytics/dimple/blob/master/src/objects/chart/methods/draw.js#L226
And then only add one axis (which would be positioned at the bottom), then manually move your axis after the drawing, like :
chart.draw();
chart.axes[0].shapes.attr('transform', ...);
But you would need to do this after every draw or else they'll be repositioned. The hack you have seems easiest.
You could open an enhancement ticket for this though : https://github.com/PMSI-AlignAlytics/dimple/issues?q=is%3Aissue+label%3Aenhancement+is%3Aopen