I have an unusual question that hopefully someone can help me with. I want to draw a wiggling or waving line with Canvas (android), but I can't get my head around how to do it. It's going to be a tadpole's tail, so ideally I want it more triangle shaped, larger at one end but that's not essential. I expect it's achievable somehow using a Path, but I'm not sure how. I've Googled around but can't find a solution, suggestions are much appreciated.
Thanks.
Create a sine wave generator that takes the phase (angle) to start from as a parameter and have it return an array of plot points ([x][y]). Then plot these points on your canvas. You can make the line wave by varying the starting phase from 0 to 360 over time. How quickly you vary x determining how fast your line waves.
Try starting here for sine wave generators:
http://www.drdobbs.com/jvm/music-components-in-java-creating-oscill/230500178
If you need a lot of tadpoles, then this might be too processor intensive in which case, you could draw a set of sine waves, say 30 with the beginning angle in steps of 12, then draw these to your canvas in sequence.
Related
I am doing a game like Geometry dash in java , I almost do all of the mechanics but I cant achieve wave effect , I try using a arrylist of lines then draw then in the points of my arrow , but it dint work or maybe I'm very confused
here is a capture of how look right now:
When I press X the arrow goes up and leave a diagonal trace (line) then if I released x its goes down I another line appear in the point of the last line , that whats is look like a wave effect of curves
here is a link of wave mechanic in geometry dash
[geometry dash wave effect][2]
Does I need to use sine wave? becaus its graph look pretty similar but I am not confident about that
I'm having a lot of problems with this mechanic and hope someone could helpe me with any idea and support
thanks for your idea i already solved it ! i finally get the wave effect c: , i draw the lines between the points of my arrow using a normal array
capture3
Don't store lines. Store points. Then draw lines between those points. At a minimum that'll stop it from breaking. Typically you're going shift those points to the left repeatedly at the same speed as the board gets shifted. You're just messing up the math, but, with points it'll at least make it very clear what you're actually getting wrong as well as keeping a contiguous set of lines.
It's not a sine wave or some bezier curves, it's a bunch of line segments as points for the previous position of your ship or whatnot changes.
I'm currently working on a small JavaFX program for stamp collectors. The idea is to allow the user to load a scan of a postage stamp and manipulate it in various ways. I've run into a small roadblock with one particular feature, however, and I'm looking for input from the experts on what approach to take.
My current problem is how to deskew the image. Here's a sample iamge.
I initially tried this method, which uses a variant of a Radon transform. It works well as long as the stamp has straight edges. With stamps that have perforations around the edges, however, it produces wildly variable results.
Because of variations within postage stamp designs, the optimal solution would be one in which I can detect the perforation circles and align them.
Before I start down another rabbit trail, does anyone have any suggestions as to what would be a good method to make this work? I don't necessarily need code (though that would be great), I'm really just looking for some guidance from those with a better understanding of what the possible options are.
Thanks in advance.
EDIT: After some additional testing, it appears the problem with the current Radon transform is not the uneven edges of the perforated stamps, but rather the initial skew angle of the image.
Anything within about 5 degrees of vertical produces a correct skew angle, and is properly rotated into alignment. Anything more than about 5 degrees generates an incorrect skew angle -- interestingly enough, the same incorrect skew angle (0.001953122516 radians) every time, regardless of whether the source image is at a 15 degree angle or 45 degrees.
I have a wave that moves according to the amplitude that the speaker is currently picking up, but if I'm quiet and then make a sudden noise, the wave "jumps" to that amplitude immediately, and I want to make the effect slightly more gradual. How can I accomplish this task?
I'm drawing the wave now using:
Canvas.drawPath(path,paint);
path.reset();
invalidate();
All in the onDraw method of my custom View. So the path is essentially drawn with whatever amplitude the mic is currently picking up (with a limit), which means that if the amplitude picked up is at say 100 right now then suddenly jumps to say 4000, the path will "jump" considerably as well.
It depends on actually what you need to do. If you want to smooth your path, i suggest you to draw a curve instead a path with some angle (you also can make with lines).
Otherwise, you can do the same using sleeps regarding that the whole transition should be small enought to fit into the frequency that you collect data.
I don't think adding sleeps would be be best solution, as you could possibly loose incoming data that you may want to graph. I'd suggest saving the last x samples and then averaging in the incoming packets. So if you're at 10 and the next few samples come in at 4000 and then back down, your average would be not nearly has high. If it rose to 4000 and stayed there, the graph would eventually rise to 4000 in a few samples, depending on how you are averaging.
Is there a way to iterate through all the points on a line in java 2D?
I've looked through the class documentation and can't see anything that does it built in. If not, would it be better to extend the class and write my own method which calculates the equation of the line and then goes through each point? (Would this actually work as the theoretical equation of a line and the pixels that it actually draws in seem to be slightly different)
For something moving on a line with a constant velocity, its coordinates are an affine function of time:
x(t) = vx*t + x0
y(t) = vy*t + y0
where (vx,vy) is the constant velocity (or speed) vector and (x0,y0) the origin position (at time 0).
I suggest reading some basic introductory course on kinematics
So you probably don't want to "iterate on the line" but simply to move something on the screen, that is to compute its position at every time quantum.
(I've learned such equations at high-school, in France)
It sounds like you are trying to do something along the lines of a bounding box. Essentially, you should have an imaginary box around your sprite. Then just check when the bounding box intersects with the line. When it does, you move your sprite in the opposite direction.
This question on Game Development Stack Exchange should help.
I have a gray scale image of 64x64. I found the dots of the contour by a simple algorithm:
find brightest spot (Example: 100)
divide by 2 (100/2 = 50)
define a band around the result (50-5=45 to 50+5=55)
mark all dots that have value in the band (between 45 to 55)
The question now is, how do I decide of the order of connecting the dots?
(All references will be accepted, links, thoughts, etc')
Your algorithm allows the entire image, save for one pixel, to be "the contour". I'm not sure that's exactly what you want; usually a contour is a border between two different regions. The problem with your method is that you can get huge blobs of pixels that have no particularly obvious traversal order. If you have a contour that is a single pixel thick, then the traversal order is much more obvious: clockwise or counterclockwise.
Consider the following image.
........
..%%%%..
.%%%%...
...%%%%.
....%...
........
Here, I've marked everything "dark" (<50, perhaps) as % and everything bright as .. Now you can pick any pixel that is on the border between the two regions (I'll pick the dark side; you can draw the contour on the light side also, or with a little more work, directly between the light and dark sides.)
........
..%%%%..
.*%%%...
...%%%%.
....%...
........
Now you try to travel along the outer edge of the dark region, one pixel at a time. First, you look in the direction of something bright (directly left, for example). Then you rotate around--counterclockwise, let's say--until you hit a dark pixel.
........
..%%%%..
1*5%%...
234%%%%.
....%...
........
Once you hit position 5, you see that it's dark. So, you mark it as part of the contour and then try find the next piece on the contour by sweeping around starting from the pixel you just came from
........
..%%%%..
.0*%%...
.123%%%.
....%...
........
Here 0 is where you came from--and you're not going back there--and then you try pixels 1 and 2 (both light, which is not okay), until you hit pixel 3, which is dark.
In this way, you can walk around the contour pixel by pixel--both identifying the contour and getting the order of pixels--until you collide with the same pixel you started with and will leave from it so that you hit the same pixel that you did the first time you left it. Then the contour is closed. In our example, where we're making an 8-connected contour (i.e. we look at 8 neighbors, not 4), we'd get the following (where # denotes a contour point).
........
..####..
.##%#...
...#%##.
....#...
........
(You have to have this two-in-a-row criterion or if you have a dark region a single pixel wide, you'll walk up it but then not be able to walk back down along it.)
At this point, you've covered one entire boundary. But there might be others. Keep looking for dark pixels next to light ones until you have drawn a contour on top of all of them. Now you've converted your two-level picture (dark & bright pixels) into a set of contours.
If the contours end up too noisy, consider blurring the image first. That will smooth the contours out. (Alternatively, you can find the contours first and then average the coordinates with a moving window.)
In general, a given set of points can be connected in multiple ways to make different shapes.
For example, consider a set of 5 points consisting of the corners of a square and its center. These points can be connected to form a square with one side "dented in" to the center. But which side? There is no unique answer.
Other shapes can be much more complicated, with no obvious way to connect the dots.
If you are allowed to reduce your set of points to a convex hull, then it would be much easier.
I have also tried to create an algorithm that will connect contour dots into the smooth curve. See my open-source project http://outliner.codeplex.com.
The idea is the same as proposed by FUZxxl but I do not understand his worries about complexity: the time of processing is proportional to the total length of all contour strokes.
I don't know if collecting those points will get you far. (I can come up with situations in which it's almost impossible to tell which order they should come in.)
How about going to the brightest point.
Go to the brightness point of, say, 360 points surrounding this point at a distance of, say, 5 pixels.
Continue from there, but make sure you don't go back where you came from :)
Maybe try:
Start at a
Find the nearest point b
Connect a with b
and so on.
Probably not good, as complexity is something like O(n²). You may simplify this by looking only for points near the start, as aioobe suggest. This algorithm is good, if the points are just like 2-3px away from each other, but may create very strange grids.
See also Flood fill
and the lovely applet under SO
mapping-a-branching-tile-path .