I have a rather complex example I wanted to try and get working, perhaps someone better at maths than me has a better approach. Code so far: https://gist.github.com/kyle-ssg/697570a7d5edc0e14cc6967b2e40b7ba.
I have a container component that scales from 1 to 4 and a child component I wish to keep in the exact same position throughout (i.e. appear not to also scale) so that I can mask it using overflow:hidden.
My approach was to essentially negate the scale and translate the x and y appropriately as the scale of the parent increases. I thought that my approach was working as I could adjust the animated value from 0 to 1 along with any widths/heights and positions and the start and finish was exactly what I expected
https://ibb.co/TTCkN8M (start) https://ibb.co/KwKfW4F (finish)
However, when animating the value between 0 and 1 it seems to start and end correctly but not animate appropriately inbetween (i.e. the red square staying in place). http://g.recordit.co/HPEkMyvVOf.gif
In the end I didn't need this, using react-native-masked-view achieved what I wanted.
Related
So i'm trying to figure out how much Y a player has fallen in a game to then figure out how much damage should be taken. I'm doing this by trying to store the current y position of a player when they aren't on the ground, getting the new y position then subtracting the new position with the old one. Problem is oldy is always set to the current y and i don't know how to keep it separate. I don't have a lot of experience with C so any help would be appreciated
if (player->grounded==false){
player->blocksfallen=player->position.y-player->oldy;
player->oldy=player->position.y;
} else {
player->blocksfallen=0;
}
The damage should depend on the speed with which the ground is hit, not by the height fallen, though there is a relation between those.
The relation is not 1:1, consider somebody starting to fall with already a downward speed, e.g. being thrown downwards. Or the other direction, the speed might be slowed by something like a parachute.
Inside the code this translates to having a variable with (at least the vertical component of) the speed. Increase it for acceleration, decrease it for any breaking effects (more precisely phrased: increase it less for breaking effects).
When you hit the ground, calculate damage based on the speed at that point.
Introducing that speed variable will also solve your problem in the absence of thrown characters and parachutes. It is then something of an accumulator of accelearation, which is very close to an accumulator of distance fallen - just easier to update. You just need to increase it by the gravity-caused acceleration each turn/frame/animation-step.
This means to turn the calculation around. Instead of trying to calculate the speed/total-height/damage from the previous height andcurrent height, you calculate the current height from previous height + speed. If you look at how you calculate the current height form previous you will notice that you already do something similar there (let's say I am pretty sure you do, though it is not seen in the shown code, something like height=height-speedconstant).
I'm recently playing with glPolygonOffset( factor, units ) and find something interesting.
I used GL_POLYGON_OFFSET_FILL, and set factor and units to negative values so the filled object is pulled out. This pulled object is supposed to cover the wireframe which is drawn right after it.
This works correctly for pixels inside of the object. However for those on object outline, it seems the filled object is not pulled and there is still lines there.
Before pulling the filled object:
After pulling the filled object:
glEnable(GL_POLYGON_OFFSET_FILL);
float line_offset_slope = -1.f;
float line_offset_unit = 0.f;
// I also tried slope = 0.f and unit = -1.f, no changes
glPolygonOffset( line_offset_slope, line_offset_unit );
DrawGeo();
glDisable( GL_POLYGON_OFFSET_FILL );
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
DrawGeo();
I read THIS POST about the meaning and usage of glPolygonOffset(). But I still don't understand why the pulling doesn't happen to those pixels on border.
To do this properly, you definitely do not want a unit of 0.0f. You absolutely want the pass that is supposed to be drawn overtop the wireframe to have a depth value that is at least 1 unit closer than the wireframe no matter the slope of the primitive being drawn. There is a far simpler approach that I will discuss below though.
One other thing to note is that line primitives have different coverage rules during rasterization than polygons. Lines use a diamond pattern for coverage testing and triangles use a square. You will sometimes see software apply a sub-pixel offset like (0.375, 0.375) to everything drawn, this is done as a hack to make the coverage tests for triangle edges and lines consistent. However, the depth value generated by line primitives is also different from planar polygons, so lines and triangles do not often jive for multi-pass rendering.
glPolygonMode (...) does not change the actual primitive type (it only changes how polygons are filled), so that will not be an issue if this is your actual code. However, if you try doing this with GL_LINES in one pass and GL_TRIANGLES in another you might get different results if you do not consider pixel coverage.
As for doing this simpler, you should be able to use a depth test of GL_LEQUAL (the default is GL_LESS) and avoid a depth offset altogether assuming you draw the same sphere on both passes. You will want to swap the order you draw your wireframe and filled sphere, however -- the thing that should be on top needs to be drawn last.
I have a standard 50-state map built with d3 in which I'm dynamically coloring states according to various datasets. Whatever the dataset, the values are normalized on a scale of 0 to 1, where 1 corresponds to the state with the highest value. I'm looking for a way to calculate the shade of the state using the value of the normalized data point.
In the past, I've chosen a base color that I like -- say, #900 -- and set the fill of each state to that color and the opacity to the normalized value. This works okay save for two problems:
when the canvas has a background color, it requires drawing a blank white state beneath every shaded state; and
fading out colors this way can look pasty
But I really like being able to set the color dynamically rather than dealing with bins for the data and preset arrays of RGB values for the gradient. So I'm wondering if there's a better way. I can take care of conversion if an alternate color system would work better.
d3 has a baked-in HSL converter, so I tried this:
// 0 <= val <= 1
function colorize(val) {
// nudge in the extremes
val = 0.2 + 0.6 * val;
return d3.hsl(0, val, 1 - val);
}
It works okay -- This is a map of fishing jobs, which are most prevalent in Maine and Oregon -- but I suspect there's a better way. Ideas?
I like what you did actually, but if you wish to do something different, you can always do a D3 scale. For example:
var scale = d3.scale.linear().domain([rangeMin, rangeMid,
rangeMax]).range(["#Color1","#Color2","#Color3"]);
And then set each state by
return scale(dataValue);
You can set your rangeMin and rangeMax variables to be the minimum and maximum values of your data. The median number, rangeMid, that I added is optional. I would suggest using this if you would like some variety in your color. I have used this scale feature to make a word frequency heatmap that came out pretty nice. I hope that I was able to help in some way!
Note: I used this with css hex values, but I believe RGB and HSL could also work.
I have to do some image processing but I don't know where to start. My problem is as follows :-
I have a 2D fiber image (attached with this post), in which the fiber edges are denoted by white color and the inside of the fiber is black. I want to choose any black pixel inside the fiber, and travel from it along the length of the fiber. This will involve comparing the contrast with the surrounding pixels and then travelling in the desired direction. My main aim is to find the length of the fiber
So can someone please tell me atleast where to start? I have made a rough algorithm in my mind on how to approach my problem but I don't know even which software/library to use.
Regards
Adi
EDIT1 - Instead of OpenCV, I started using MATLAB since I found it much easier. I applied the Hough Transform and then Houghpeaks function with max no. of peaks = 100 so that all fibers are included. After that I got the following image. How do I find the length now?
EDIT2 - I found a research article on how to calculate length using Hough Transform but I'm not able to implement it in MATLAB. Someone please help
If your images are all as clean as the one you posted, it's quite an easy problem.
The very first technique I'd try is using a Hough Transform to estimate the line parameters, and there is a good implementation of the algorithm in OpenCV. After you have them, you can estimate their length any way you want, based on whatever other constraints you have.
Problem is two-fold as I see it:
1) locate start and end point from your starting position.
2) decide length between start and end points
Since I don't know your input data I assume it's pixel data with a 0..1 data on each pixel representing it's "whiteness".
In order to find end points I would do some kind of WALKER/AI that tries to walk in different locations, knowing original pos and last traversed direction then continuing along that route until "forward arc" is all white. This assumes fiber is somewhat straight (is it?).
Once you got start and end points you can input these into a a* path finding algorithm and give black pixels a low value and white very high. Then find shortest distance between start and end point, that is the length of the fiber.
Kinda hard to give more detail since I have no idea what techniques you gonna use and some example input data.
Assumptions:
-This image can be considered a binary image where there are only 0s(black) and 1s(white).
-all the fibers are straight and their starting and ending points are on borders.
-we can come up with a limit for thickness in fiber(thickness of white lines).
Under these assumptions:
start scanning the image border(start from wherever you want in whichever direction you want...just be consistent) until you encounter with the first white pixel.At this point your program will understand that this is definitely a starting point. By knowing this, you will gather all the white pixels until you reach a certain limit(or a threshold). The idea here is, if there is a fiber,you will get the angle between the fiber and the border the starting point is on...of course the more pixels you get(the inner you get)the surer you will be in the end. This is the trickiest part. after somehow ending up with a line...you need to calculate the angle(basic trigonometry). Since you know the starting point, the width/height of the image and the angle(or cos/sin of those) you will have the exact coordinate of the end point. Be advised...the exactness here is not really what you might have understood because we may(the thing is we will) have calculation errors in cos/sin values. So you need to hold the threshold as long as possible. So your end point will not be a point actually but rather an area indicating possibility that the ending point is somewhere inside that area. The rest is just simple maths.
Obviously you can put too much detail in this method like checking the both white lines that makes the fiber and deciding which one is longer or you can allow some margin for error since those lines will not be straight properly...this is where a conceptual thickness comes to the stage etc.
Programming:
C# has nice stuff and easy for you to use...I'll put some code here...
newBitmap = new Bitmap(openFileDialog1.FileName);
for (int x = 0; x < newBitmap.Width; x++)
{
for (int y = 0; y < newBitmap.Height; y++)
{
Color originalColor = newBitmap.GetPixel(x, y);//gets the pixel value...
//things go here...
}
}
you'll get the image from a openfiledialog and bitmap the image. inside the nested for loop this code scans the image left-to-right however you can change this...
Since you know C++ and C, I would recommend OpenCV
. It is open-source so if you don't trust anyone like me, you won't have a problem ;). Also if you want to use C# like #VictorS. Mentioned I would use EmguCV which is the C# equivilant of OpenCV. Tutorials for OpenCV are included and for EmguCV can be found on their website. Hope this helps!
Download and install the latest version of 3Dslicer,
Load your data and go the the package>EM segmenter without Atlas>
Choose your anatomical tree in 2 different labels, the back one which is your purpose, the white edges.
The choose the whole 2D image as your ROI and click on segment.
Here is the result, I labeled the edges in green and the black area in white
You can modify your tree and change the structures you define.
You can give more samples to your segmentation to make it more accurate.
You can see the code there: http://jsfiddle.net/jocose/CkL5F/901/
(double click on the box and move your mouse)
NOTE: This is a simplified example that is part of a larger system. My ultimate goal is to manipulate individual vertices of a path.
Update: I crunched the numbers and the math actually apears to be correct. What I want to do is calculate the offset from each point to the mouse, and then move that point to the mouses position + the offset.
So if I have a mouse of 224 then 224-103 = 121 then I add: 121+224=345
These creates a cycle of ups and downs that I am seeing. I don't know why these is stumping me so badly, any help would be much appreciated.
I need to manually update a Raphael path element.
To do this I convert an absolute path into an array using Raphael great built in function "parsePathString"
I then loop through that array and modify the values based off the mouse position.
The update is done to the X values only, and is in real time; called each time the mouse moves.
When the element moves it flickers back and forth between the correct position and some anomalous one.
I have no clue why its doing this. I have spent almost 5 hours trying to figure this out and I'm officially stuck.
Here is a sample of the result where you can see the values jumping around:
MOUSE224
M,103.676287
MOUSE225
M,346.323713
MOUSE227
M,107.676287
MOUSE228
M,348.323713 12
MOUSE228
M,107.676287
MOUSE229
M,350.323713
MOUSE231
M,111.67S287
MOUSE232
M,3S2.323713
MOUSE233
M,113.676287
MOUSE233
M,3S2.323713
Here's my version of your fiddle modified to do what I think you need. At least, it seems to work. It's the same type of problem I had to fix for the Raphael 2 transformations here.
Basically, in your mousemove, I've changed mx to be a calculation of the offset between where your mouse is now and where it was the last time mousemove was called. Your move() function now only has to add this value to the x-coords.
Hope this helps you out somewhat