How to determine endpoints of Arcs in GraphicsPath PathPoints and PathTypes arrays? - gdi+

I have the following PathPoints and PathTypes arrays (format: X, Y, Type):
-177.477900, 11021.670000, 1
-614.447200, 11091.820000, 3
-1039.798000, 10842.280000, 3
-1191.761000, 10426.620000, 3
-1591.569000, 10493.590000, 3
-1969.963000, 10223.770000, 3
-2036.929000, 9823.960000, 3
-2055.820000, 9711.180000, 3
-2048.098000, 9595.546000, 3
-2014.380000, 9486.278000, 3
Here is what this GraphicsPath physically looks like. The 2 Arcs are very distinguishable:
I know that this GraphicsPath.PathData array was created by 2 AddArc commands. Stepping through the code in the debugger, I saw that the first 4 PathData values were added by the first AddArc command, and the remaining 6 points were added by the 2nd AddArc command.
By examining the raw pathpoints/pathtype arrays (without previously knowing that it was 2 AddArc commands so I would know that I have 2 start and end points), I would like to determine to start and end point of each arc.
I have tried several Bezier calculations to 'recreate' the points in the array, but am at a loss to determine how to determine the separate start and end points. It appears that GDI+ is combining the start/end point between the arcs (they are the same point and the arcs are connected), and I am losing the fact that one arc is ending and other one is starting.
Any ideas?

Use the GraphicsPathIterator class in combination with the GraphicsPath.SetMarkers method.
For example:
dim gp as new GraphicsPath
gp.AddArc(-50, 0, 100, 50, 270, 90) 'Arc1
gp.SetMarkers()
gp.AddArc(0, 25, 100, 50, 270, 90) 'Arc2
Dim iterator as New GraphicsPathIterator(gp)
Dim i as Integer = 0
Dim MyPts(3) As PointF
Dim temp as New GraphicsPath
Do until i > 2
iterator.NextMarker(temp)
MyPts(i) = temp.PathPoints(0)
MyPts(i + 1) = temp.GetLastPoint()
i += 2
Loop
'Free system resources...
iterator.Dispose()
temp.Dispose()
Arc1 -> start: MyPts(0); end: MyPts(1)
Arc2 -> start: MyPts(2); end: MyPts(3)
Hope this helps!

Take a look at the PathPointType Enum (System.Drawing.Drawing2D).
Value Meaning
0 Start (path)
1 Line
3 Bezier/Bezier3
7 PathType Mask
16 Dash Mode
32 Path Marker
128 Close Subpath

This one was bugging me a lot too! I had path created beyond my control without markers and couldn't figure out curve endpoints.
In this case you'd expect that the curve starts at [i + 1] but it is not! It turns out that GDI combines path points probably to make the points array shorter. In this case the curve points are: [0], [1], [2], [3].
It seems that if PathPointType.Start or PathPointType.Line is followed by PathPointType.Bezier, then you have to treat the PathPontType.Start or Path.PointType.Line as a first point of your Bezier curve, so in your example it should be like this:
-177.47, 11021.67, 1 // Draw line to this point AND use it as a Bezier start!
-614.44, 11091.82, 3 // Second Bezier point
-1039.79, 10842.28, 3 // Third Bezier point
-1191.76, 10426.62, 3 // Fourth Bezier point AND first point of the next Bezier!
-1591.56, 10493.59, 3 // Second Bezier point
-1969.96, 10223.77, 3 // Third Bezier point
-2036.92, 9823.96, 3 // Fourth Bezier point AND first point of the next Bezier!
-2055.82, 9711.18, 3 // Second Bezier point
-2048.09, 9595.54, 3 // Third Bezier point
-2014.38, 9486.27, 3 // Fourth Bezier point
So, when analysing PathPoints array point by point, you have to also check current and following indices. The docs on PatPointType might come in handy. In most cases you can probably ignore additional data stored on bits other than the first three (these three define Start, Line and Bezier). The only exception is CloseSubpath but it's irrelevant if you consider the next advice.
It's also worth noting that if you have a complex path that consists of huge number of PathPoints then it might be handy to split the path into chunks using GraphicsPathIterator. This simplifies the whole procedure as PathPointType.CloseSubpath can be ignored - it will be always the last point of your GraphicsPath chunk.
A quick look into Reflector or here might be helpful if you want to better understand PointTypes array.

Related

How to calculate steps needed for arrow to fill all given sectors of the clock?

the problem's data are:
Analog clock is dived into 512 even sections, arrow/handle starts its movement at 0° and each tick/step moves it by 4.01°. Arrow/Handle can move only clockwise. What minimum ticks/steps count is needed for arrow/handle to visit all sections of the clock.
I'm trying to write a formula to calculate the count but can't quite wrap my head around it.
Is it possible to do it? If yes, how can I do it?
This site is for programmers, isn't it?
So we can hire our silicon friend to work for us ;)
Full circle is 360*60*60*4=5184000 units (unit is a quarter of angular second)
One step is 4*(4*3600+36) = 57744 units
One section is 4*360*3600/512 = 10125 units (we use quarters to make this value integer)
cntr = set()
an = 0
step = 57744
div = 10125
mod = 5184000
c = 0
while len(cntr) < 512:
sec = (an % mod) // div
cntr.add(sec)
an += step
c += 1
print(c)
>>804
unfortunately I can`t fully answer your question but the following may help:
Dividing the 512 Sections into degree gives you 1,4222° each.
Each round you cover 90 different section when starting between 0°-3.11° and 89° when starting between 3.12°-4.00°
For starting the rounds this gives you a change in starting degree of 0.9° every round except after the fourth, where it is only 0.89°(within the possible range of 0°-4° so all calculated mod 4).
So you have 0.9°->1.8°->2.7°->3.6°->0.49->1.39°...0.08°...
I hope this helps you devloping an algorithm

R circular "linked" list: adding +1 to last index brings you to first index

I'm trying to implement movement through four points, while recording which points I visit. Think of it as a square. I can move from corner to corner or diagonally.
If you 'unwrap' the square you get a straight line with four points, which can be thought of as 1-2-3-4- where after 4 it goes back to 1. So if I'm at point 2 I can move to 1 and 3 directly or 4 diagonally. I'd implement that as 2-1 / 2+1 for corner-to-corner or 2+/-2 for diagonally. The problem occurs when I'm at 2 and will try to subtract 2 where I'll end up outside of the list.
The thought I've had is that if I could somehow translate my "out of bounds" numbers to in bounds this would be solved. One solution is hard coding that:
0=4
-1=3
5=1
6=2
but I'm pretty sure there is a better way to do this, however I can't seem to find it.
It seems to me all you want is modular arithmetic (bless the lord for math)
magicFun <- function (x) x %% 4
Here is a simple test run
> magicFun(0:6)
[1] 0 1 2 3 0 1 2
Addendum
It's more about math but the reason it works for negatives is that in Z/nZ ("the world where n is equal to 0") n is "identified" to 0.
This means you can add n as many times as you wish to a given number without changing it's "value".
Also, by convention the numbers in Z/nZ are listed as {0, 1, ..., n-1}.
So suppose n = 4 and x = -6, by the above x = x + 2*4 = 2.

Completing a list of possible binary sequences give a binary sequence with gaps

So, I am working on a program in Scilab which solves a binary puzzle. I have come across a problem however. Can anyone explain to me the logic behind solving a binary sequence with gaps (like [1 0 -1 0 -1 1 -1] where -1 means an empty cell. I want all possible solutions of a given sequence. So far I have:
function P = mogelijkeCombos(V)
for i=1:size(V,1)
if(V(i) == -1)
aantalleeg = aantalleeg +1
end
end
for i=1:2^aantalleeg
//creating combos here
end
endfunction
sorry that some words are in dutch
aantalleeg means amountempty by which I mean the amount of empty cells
I hope I gave you guys enough info. I don't need any code written, I'd just like ideas of how I can make every possible rendition as I am completely stuck atm.
BTW this is a school assignment, but the assignment is way bigger than this and it's just a tiny part I need some ideas on
ty in advance
Short answer
You could create the combos by extending your code and create all possible binary words of the length "amountempty" and replacing them bit-for-bit in the empty cells of V.
Step-by-step description
Find all the empty cell positions
Count the number of positions you've found (which equals the number of empty cells)
Create all possible binary numbers with the length of your count
For each binary number you generate, place the bits in the empty cells
print out / store the possible sequence with the filled in bits
Example
Find all the empty cell positions
You could for example check from left-to-right starting at 1 and if a cell is empty add the position to your position list.
V = [1 0 -1 0 -1 1 -1]
^ ^ ^
| | |
1 2 3 4 5 6 7
// result
positions = [3 5 7]
Count the number of positions you've found
//result
amountempty = 3;
Create all possible binary numbers with the length amountempty
You could create all possible numbers or words with the dec2bin function in SciLab. The number of possible words is easy to determine because you know how much separate values can be represented by a word of amountempty bits long.
// Create the binary word of amountEmpty bits long
binaryWord = dec2bin( i, amountEmpty );
The binaryWord generated will be a string, you will have to split it into separate bits and convert it to numbers.
For each binaryWord you generate
Now create a possible solution by starting with the original V and fill in every empty cell at the position from your position list with a bit from binaryWordPerBit
possibleSequence = V;
for j=1:amountEmpty
possibleSequence( positions(j) ) = binaryWordPerBit(j);
end
I wish you "veel succes met je opdracht"

Flex and polygonal.de graph classes in pathfinding?

I am wondering if someone have done this already, to send me into right direction..
The issue is as follow : I have a 2 dimensional array, on which i hold integer numbers, if the number is 0 - the item shall not be included into the graph, if it is 1 - it must be included.
The result graph shall be used for patfinding ( the shortest path ) to some element.
how to turn this 2 dimensional array into the graph ? ( with polygona.de classes if possible ),
I am currently trying with Polygonal.de classes. any suggestions and points into the right direction is more than appreciated.
This is the 2 dimensional structure. The red cells is prohibited to walk on, and there must be found optimal path from "start" to the "end". But 1st things 1st - i need to turn this 2 dimensional structure into a graph now :|
The way I see it, your 2D array is already a graph. A node of the graph is represented by a pair (i, j) and may have neighbor nodes such as (i + 1, j), (i, j + 1), etc. You can write a utility function for your array that hides these low-level neighbor definitions and skips the cells that are occupied.
The de.polygonal.ds API for the Graph data structure contains this example for the construction of a graph:
var graph = new de.polygonal.ds.Graph<String>();
var a = graph.addNode("a");
var b = graph.addNode("b");
var c = graph.addNode("c");
graph.addSingleArc(a, b, 1.0);
graph.addSingleArc(b, a, 1.0);
graph.addMutualArc(a, c, 1.0);
Adjust the example to construct a 2D array that contains a node for each free (i, j) of the original 2D array. Then traverse the 2D array of nodes and call addMutualArc() to connect adjacent nodes.

algorithm to traverse 3D coordinates without revisiting

Say we have a set of 3D (integer) coordinates from (0,0,0) to (100,100,100)
We want to visit each possible coordinate (100^3 possible coordinates to visit) without visiting each coordinate more than once.
The sum of the differences between each coordinate in adjacent steps cannot be more than 2 (I don't know if this is possible. If not, then minimized)
for example, the step from (0,2,1) to (2,0,0) has a total difference of 5 because |x1-x2|+|y1-y2|+|z1-z2| = 5
How do we generate such a sequence of coordinates?
for example, to start:
(0,0,0)
(0,0,1)
(0,1,0)
(1,0,0)
(1,0,1)
(0,0,2)
(0,1,1)
(0,2,0)
(1,1,0)
(2,0,0)
(3,0,0)
(2,0,1)
(1,0,2)
(0,0,3)
etc...
Anyone know an algorithm that will generate such a sequence to an arbitrary coordinate (x,y,z) where x=y=z or can prove that it is impossible for such and algorithm to exist? Thanks
Extra credit: Show how to generate such a sequence with x!=y!=z :D
One of the tricks (there are other approaches) is to do it one line [segment] at a time, one plane [square] at a time. Addressing the last part of the question, this approach works, even if the size of the volume visited is not the same in each dimension (ex: a 100 x 6 x 33 block).
In other words:
Start at (0,0,0),
move only on the Z axis till the end of the segment, i.e.
(0,0,1), (0,0,2), (0,0,3), ... (0,0,100),
Then move to the next line, i.e.
(0,1,100)
and come backward on the line, i.e.
(0,1,99), (0,1,98), (0,1,97), ... (0,1,0),
Next to the next line, going "forward"
And repeat till the whole "panel is painted", i.e ending at
... (0,100,99), (0,100,100),
Then move, finally, by 1, on the X axis, i.e.
(1,100,100)
and repeat on the other panel,but on this panel going "upward"
etc.
Essentially, each move is done on a single dimension, by exactly one. It is a bit as if you were "walking" from room to room in a 101 x 101 x 101 building where each room can lead to any room directly next to it on a given axis (i.e. not going joining diagonally).
Implementing this kind of of logic in a programming language is trivial! The only mildly challenging part is to deal with the "back-and-forth", i.e. the fact that sometimes, some of the changes in a given dimension are positive, and sometimes negative).
Edit: (Sid's question about doing the same diagonally):
Yes! that would be quite possible, since the problem states that we can have a [Manhattan] distance of two, which is what is required to go diagonally.
The path would be similar to the one listed above, i.e. doing lines, back-and-forth (only here lines of variable length), then moving to the next "panel", in the third dimension, and repeating, only going "upward" etc.
(0,0,0) (0,0,1)
(0,1,0) first diagonal, only 1 in lengh.
(0,2,0) "turn around"
(0,1,1) (0,0,2) second diagonal: 2 in length
(0,0,3) "turn around"
(0,1,2) (0,2,1) (0,3,0) third diagonal: 3 in length
(0,4,0) turn around
etc.
It is indeed possible to mix-and-match these approaches, both at the level of complete "panel", for example doing one diagonally and the next one horizontally, as well as within a given panel, for example starting diagonally, but when on the top line, proceeding with the horizontal pattern, simply stopping a bit earlier when looping on the "left" side, since part of that side has been handled with the diagonals.
Effectively this allows a rather big (but obviously finite) number of ways to "paint" the whole space. The key thing is to avoid leaving (too many) non painted adjacent area behind, for getting back to them may either bring us to a dead-end or may require a "jump" of more than 2.
Maybe you can generalize Gray Codes, which seem to solve a special case of the problem.
Seems trivial at first but once started, it is tricky! Especially the steps can be 1 or 2.
This is not an answer but more of a demostration of the first 10+ steps for a particular sequence which hopefully can help others to visualise. Sid, please let me know if the following is wrong:
s = No. of steps from the prev corrdinates
c1 = Condition 1 (x = y = z)
c2 = Condition 2 (x!= y!= z)
(x,y,z) s c1 c2
---------------
(0,0,0) * (start)
(0,0,1) 1
(0,1,0) 2
(1,0,0) 2
(1,0,1) 1
(1,1,0) 2
(1,1,1) 1 *
(2,1,1) 1
(2,0,1) 1 *
(2,0,0) 1
(2,1,0) 1 *
(2,2,0) 1
(2,2,1) 1
(2,2,2) 1 *
(2,3,2) 1
(2,3,3) 1
(3,3,3) 1 *
(3,3,1) 2
(3,2,1) 1 *
(3,2,0) 1 *
.
.
.

Resources