Accessing elements of a NSBezierPath - cocoa

I am just starting to learn how to work with NS-classes.
Right now I am trying to figure out how it is possible to index through and access certain parts of a NSBezierPath.
In the snippet below there is a circular path that has 12 nodes and 4 segments. I would like to be able to know how to index and cycle through each of those nodes/segments and apply thickness/color. I can tell the parts in the code that make up the segments but I don't know how to access them.
Simply put, as an example, I would like to be able to:
- select a path
- select it's 3rd segment
- give it some thickness
- color it blue
I been looking at the AppKit documentation and it seems the codes below are what I need but I honestly have no idea of how to apply them.
func element(at index: Int) -> NSBezierPath.ElementType
func element(at index: Int,
associatedPoints points: NSPointArray?) -> NSBezierPath.ElementType
https://developer.apple.com/documentation/appkit/nsbezierpath/1520751-element?changes=_5
I would greatly appreciate any help and examples to better understanding how to work with these methods and apply them.
SNIPPET:
for path in layer.paths:
bp = path.bezierPath
bp.setLineWidth_(5)
NSColor.blueColor().set()
bp.stroke()
print bp
PRINTS OUT THIS:
Path <0x60400553f0e0>
Bounds: {{157, 0}, {286, 286}}
Control point bounds: {{157, 0}, {286, 286}}
300.000000 0.000000 moveto
379.000000 0.000000 443.000000 64.000000 443.000000 143.000000 curveto
443.000000 222.000000 380.000000 286.000000 300.000000 286.000000 curveto
221.000000 286.000000 157.000000 222.000000 157.000000 143.000000 curveto
157.000000 64.000000 221.000000 0.000000 300.000000 0.000000 curveto
closepath
300.000000 0.000000 moveto

Related

How to used linspace matlab function to get the and the y of all pixels in image?

I have an image of 200x200 * 3, and I should to find the x and the y of each pixel of the image by using the linspace Matlab function.
I have already used the mesh grid function with the following code:
Ny=200; % # pixels in y direction
Nx=200; % # pixels in x direction
resolution=1; % size of a pixel
img=rand(Ny,Nx,3); % random image
y=(img(1:Ny))*resolution-resolution/2;
x=(img(1:Nx))*resolution-resolution/2;
[x,y]=meshgrid(x,y);
But my supervisor told me to use the function linspace and I cannot understand how.Can someone help me please
The only reason I can think of that your supervisor wants you to use linspace is for creating the x and y vector inputs to meshgrid. linspace alone is not sufficient to generate all of the pixel coordinates, so you'll have to use meshgrid, ndgrid, repelem or repmat.
The one advantage that linspace has over simply doing something like 1:resolution:Ny is that linspace ensures that the endpoints always appear in the range. For example, if you do
>> 1:.37:4
ans =
1.0000 1.3700 1.7400 2.1100 2.4800 2.8500 3.2200 3.5900 3.9600
you don't get 4 as the last point. However, if you use linspace:
>> linspace(1,4,9)
ans =
1.0000 1.3750 1.7500 2.1250 2.5000 2.8750 3.2500 3.6250 4.0000
the spacing is automatically adjusted to make the last element 4.
So assuming that resolution is the multiplier for the number of points you want, for 2 you want 401 evenly spaced points and for 1/2 you want 101 points, your code would look something like this:
x_points = linspace(1, Nx, Nx*resolution+1);
y_points = linspace(1, Ny, Ny*resolution+1);
(I'm still not sure what the resolution/2 part is supposed to do, so we'll need to clarify that.)
Then you would pass those to meshgrid:
[X,Y] = meshgrid(x_points, y_points);

How do you calculate the mid-point between two CGAffineTransform scale transforms?

This might be more of a math question than a programming question, but in this example it's within the context of CGAffineTransform.
If you have two transforms that each represent a scaling transform, how do you calculate a mid-point between the two that would visually represent "half way" to the user?
For example:
CGAffineTransform minimumScale = CGAffineTransformMakeScale(1, 1);
CGAffineTransform maximumScale = CGAffineTransformMakeScale(10, 10);
Creating a transform with a scale factor of 5 does not produce the result I would like because going from 1 -> 5 represents a "5x magnification" from the user's perspective but going from 5 -> 10 only represents a "2x magnification".
In this trivial example, a scale factor of roughly 3.33 would produce the result I'm looking for as it would transform a frame like so:
W:100 x H:100 -> 333 x 333 // A 3x magnification from the user's perspective
W:333 x H:333 -> 999 x 999 // Also a 3x magnification from the user's perspective
However, the math formula eludes me for calculating this value given any two minimum and maximum scale values (of which the minimum could be less than 1.0, e.x.: 0.2 -> 3.9).

Probability map of an image

Probability map of an image can be calculated as bellow, may be it help someone
1
We have a probability matrix which has probabilty for each class. Let this be
x=[x1 x2 x3 x4] for 4 classes
2
to get probability map we will have four separate images for each class. let for class x1
x1=x1*255. this will be the pixel value for labeling.
3
Now for each class we will multiply this 255 with each probability value and will set value in the image this one.
4
as a result we will have four gray scale images and these are called probability maps.
You could generate 6 maps for all classes. Or you could select 6 different colors, and use the probability to interpolate a new color, then you will get just one map.

Selective Ruby array slicing

I've been slowly learning Ruby (at this point, maybe the first language I've invested any amount of time in actually learning) so this is probably going to be a very simple question for many of you.
My toy project for my studies is, basically, a roguelike. Currently, I have a Map class that contains an array of Tile objects representing, of course, each tile in the entire map. I'm attempting to create a method that will return a smaller array (most likely example would be to display the currently viewable area of the map).
My problem comes down to this. Since the array containing all these tiles is single-dimension, I can't seem to think of a clean way to slice pieces of this array out based on two x,y coordinates that the method takes to determine what to return. In other words, I can't seem to find a clean way to translate between the two coordinate pairs without some fairly ugly looking code and I get the idea that there's a very simple way to do this that is just not 'clicking'.
Ideas anyone? I'm open to some pretty crazy suggestions!
If your array is single dimention you can map x and y coords to an array index much like you do pixels on a vga buffer.
offset = y * buffer_width + x
If your map tile width is 100 tiles and you wanted to get tile 5,5 then 5 * 100 + 5 = array index 505 would be the corresponding tile in your 1 dimensional array.
You can use this to piece together a viewable region on screen. For instance a 10x10 viewable tiles: Start at your offset and grab the next 10 items, add buffer_width to drop a row and add the next 10, and so on until you have all 10 rows for your 10x10 viewable area.
Here is an example on a smaller tile set with a tile buffer width of 5 and selecting a viewable 3x3 area:
buffer = ['0,0', '1,0', '2,0', '3,0', '4,0',
'0,1', '1,1', '2,1', '3,1', '4,1',
'0,2', '1,2', '2,2', '3,2', '4,2',
'0,3', '1,3', '2,3', '3,3', '4,3',
'0,4', '1,4', '2,4', '3,4', '4,4']
buffer_width = 5
buffer_height = 5
# now lets say we want to grab a 3x3 slice from right
# in the middle of the array from 1,1->3,3
x1,y1 = 1,1
x2,y2 = 3,3
view_width = x2 - x1
view_height = y2 - y1
(0..view_height).each do |row|
offset = (y1 + row) * buffer_width + x1
puts buffer[offset..offset+view_width].inspect
end
Our result will be an output like this:
["1,1", "2,1", "3,1"]
["1,2", "2,2", "3,2"]
["1,3", "2,3", "3,3"]
Which you can string together in a new single dimensional or multi dimensional, which ever you see fit.
Hope this helps.
You'll need to use use map on a slice of the array and map each row to a slice itself.
a = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
a[1..2].map { |row| row[0..1] }
=> [["d", "e"], ["g", "h"]]

Spatial Rotation in Gmod Expression2

I'm using expression2 to program behavior in Garry's mod. Expression2 (archive link)
Okay so, to set the precedent. In Gmod I have a block and I am at a complete loss of how to get it to rotate around the 3 up, down and right vectors (Which are local. ie; if I pitch it 45 degrees the forward vector is 0.707, 0.707, 0). Essentially, From the 3 vectors I'd like to be able to get local Pitch/Roll/Yaw. By Local Pitch Roll Yaw I mean that they are completely independent of one another allowing true 3d rotation. So for example; if I place my craft so its nose is parallel to the floor the X,Y,Z would be 0,0,0. If I turn it parallel to the floor (World and Local Yaw) 90 degrees it's now 0, 0, 90. If I then pitch it (World Roll, Local Pitch) it 180 degrees it's now 180, 0, 90. I've already explored quaternions however I don't believe I should post my code here as I think I was re-inventing the wheel.
I know I didn't explain that well but I believe the problem is pretty generic. Any help anyone could offer is greatly appreciated.
Oh, I'd like to avoid gimblelock too.
Essentially calculating the rotation around each of the crafts up/forward/right vectors using the up/forward/right vectors.
To simply the question a generic implementation rather than one specific to Gmod is absolutely fine.
I'm not sure what the application you are looking forward to implementing, however, in this sort of situation, I would usually suggest applying angular force. Would that be sufficient for your needs in this regard?
Well if that is all that you need, then i have managed to perfect the angular force equation to having entities point at a given position.
EntityVector = Entity:massCenter()
Leverage = sqrt( ( Entity:inertia():length()^2 ) / 3 )
LookPos = EntityVector - Target:pos()
A = ang(
toDeg( atanr( LookPos:z() , sqrt( LookPos:x()^2 + LookPos:y()^2) ) ) ,
toDeg( atanr( -LookPos:y(), -LookPos:x() ) ) ,
0 )
EntityAngle = ( ( Entity:angles() - angnorm(A) ) * 5 + Entity:angVel() ) * 5
Entity:applyAngForce( -EntityAngle * Leverage )
This set of equations has helped me through countless projects

Resources