Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'd like to be able to generate a bell curve to use for probability in procedural generation in games.
For, example if I want to generate a forest, I could give it a radius and intensity and make a bell curve of the right shape to give me the probability of whether a tree should be placed or not. I should end up with a lot of trees in the centre and they would become less frequent as you approach the radius distance out from the centre.
I've done something similar before using a sine wave. But I have no idea how to make a bell curve. These equations are greek to me. I have forgotten how to read them, but it would be greek to the computer anyway.
Could someone write down the equation for bell curve in C# (or python would be my 2nd choice) and maybe explain it a bit?
Sure.
p(x) = exp(-(x-mu)^2/(2*sigma^2))/sqrt(2*pi*sigma^2)
The bell curve is also called the gaussian probability distribution. It is basically taking e to the power of a negative square of the x value. The rest is to make sure that it is centered at mu and scaled to the specifics of the particular problem you are modeling, and to make sure that the integral over all values of x sums to 1.
To generate random samples from this distribution in Python, you can do something like the following:
import random
sample = random.gauss(mu, sigma)
# where mu is the center of the bell curve, and sigma is proportional to its "width"
If you want to have a two-dimensional bell curve, it's nice to know that you can find X and Y values separately like in the above, and the 2D plot will be a 2D bell curve, where the density is highest at the center (mu_x, mu_y).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have an array of numbers up to 20000 and I'm trying to assign a weight to these numbers:
The closer a number is to 0 the higher should the weight be. My problem is that I'm trying to make it such that the higher the number is, the smaller should the difference in weight be, for example the weight difference between 1-100 might be 1.5 but the difference between 100-10000 might be 0.5.
I think it's a logarithmic scale, isn't it? I'm not great at math at all.. this is not a homework question, school was out long ago just a hobby question.
What I've tried is that I've mapped weights to my number array by doing a square root on 25000-value but this isn't what I'm looking for. I just put that in so I could see a gradient of weights coming back plus the numbers are just to big, ideally I want the weights between 0.01 and 3.
I don't have any code to show, any help would be appreciated.
While your question isn't really a C# question, I may have an answer for you.
To scale a value with logarithmic spacing, you can use the following formula:
You said you maximum value is 20000 and you want to scale the values from 0.01 to a maximum of 3, so we need to insert the max and scale our formula:
// edit: also the values should be reversed, so subtract the log from 1:
This gives the following values f(x) for values of x:
f(0) = 3
f(1) = 2.79
f(10) = 2.27
f(100) = 1.60
f(1000) = 0.91
f(10000) = 0.21
f(20000) = 0
Would that suffice for your case?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
First of all, let me make it clear that I don't know much about programming. So after I got that out of the way, thanks for reading my question.
So what I currently want to cram into my little C# programm is the following:
Draw a line from pA to pX
Draw a curve from pX to pY
Draw a curve from pY to pZ
Draw a line from pZ to pD
My problem with this is the following:
How on earth do I "switch" from a line to a curve, to another curve and then back to a line in C#?
I'd be really happy if anyone could help me with this.
Greetings from Belgium,
-g2609
Seems you want to provide smooth connection of line segments and curves.
Note that Bezier curves at the end points have direction (tangents) to control points. So just put control points at the continuation of straight segments. Distance from the enpoints to the control ones is responsible for curvature. Try to use values like distXY / 3 to start.
For curve-curve connection you have to define some rule. For example, define tangent direction (and maginute again). If you need smooth curve chain, consider interpolation splines - this approach calculates cubic curves parameters for all curves and provides continuity.
Pseudocode for line A-X, cubic Bezier X-Y, line Y-Z.
VecAX = X - A
uAX = (VecAX.X / VecAX.Length, VecAX.Y / VecAX.Length)
curveXY.P0 = X
curveXY.P1 = X + uAX * VecAX.Length / 3
curveXY.P2 = Y - uXZ * VecXZ.Length / 3
curveXY.P3 = Y
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Let's say I have few points : -5,-4,-3,-2,-1,0,1,2,3,4,5
I'm at point 0, I need to create a line that goes all through the points of 1,2,3,4,5,-1,-2... etc.
The line would start at 0 and end at whatever point that ends as the shortest.
The answer for this example would be that it'd go like this 0->1->2->3->4->5->-1->-2->-3->-4->-5 or that it'd go first to -1 and go all through the minus to the plus, same result (5*4=20 length).
If for example we'd go 0->1->-1->2->-2... it'd end as the longest line that goes straight from point to point (1+2+3+4+5+6+7+8+9+10=10*11/2=55 length)
The question is how to write this in code?
The points might also consist of 2 or 3 dimensional points, where the start would be (0,0,0,0) or whatever, eventually the line can go through all of these points, but which way will achieve the shortest line?
How to make it as a code, as we see it in the eye?
I think this is basically the Travelling Salesman problem. You've got N destinations, and each pair of destinations has a concrete length between them, and you're trying to find out the shortest travel time to visit all destinations.
You've got two different directions to pursue this, that I can see. First, is to read up on the Travelling Salesman problem and the various algorithms that have been proposed for it (it's a very famous algorithm problem) and then try to implement one in C# - though just to warn you, you should be very proficient in math, because it's not an easy problem. Or, alternatively, you can look for someone else's existing implementation for it and just use it without understanding the theoretical underpinnings.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This question is regarding WPF.
I have two balls, one static and the other is moving towards the static ball. On collision the balls would move in the direction that would be decided by the collision that at which angle they would collide and move accordingly.
My question is basically for suggestions for what is the easiest and the most effective way of colliding the objects in WPF and after collision giving them a path to move accordingly. Apart from deceleration etc, what should be the best strategy that should be applied to DETECT collision and GIVE NEW PATHS TO THE BALLS AFTER COLLISION.
Simply figure out the distance of the centers of the 2 balls.
if (distance(ball1.Center, ball2.Center) <= ball1.Radius + ball2.Radius)
{
// collision
}
For the distance use this:
double x = ball1.Center.X - ball2.Center.X;
double y = ball1.Center.Y - ball2.Center.Y;
double distance = Math.sqrt(x*x + y*y); // pythagoras
For calculating the new directions you'll need some more math. Have a look for a geometrics library like it is delivered with XNA. Try googeling your question again and use XNA in the search instead of WPF - this will solve your problem, i guess.
I would calculate the distance (pythagoras) between them, if the distance is smaller than the two radius added, a collision occurs.
Check here for the angle between the points:
Math Calculation to retrieve angle between two points?
You could check this one:
Ball to Ball Collision - Detection and Handling
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I am looking for a memory-efficient yet convenient data structure for a 3D mesh or face-set consisting of triangles.
Currently I am using this 'classical' structure:
a list of points, and a list of triangles.
Each point has a X, Y, and Z value.
Each triangle has three indices i0, i1, i2, which refer to a point in the point list.
This is the most compact layout I can think of. It is perfect if all I want to do is draw the mesh, and never modify or filter it.
However it does make most operations that modify the mesh or generate a new partial mesh very cumbersome, for example:
Removing triangles is very inefficient.
generating a new mesh with only triangles that have less than 3 neighbors
Finding and removing all triangles that have one or all point within a given bounding box
finding all edges with a certain angle
removing all edges shorter than a certain length
Basically anything that requires modifying the mesh, or iterating over the edges or finding neighboring faces/edges, requires generating and discarding several temporary dictionaries and hash sets. There is no easy way to iterate over the points or edges of an individual face, or the edges/faces around an individual point. removing a point means removing it from each triangle, then changing the index values for all other points in all triangles, etc.
Is there a canonical data structure that does not have these drawbacks, yet is memory-efficient?
I am not looking for an entire library, just a structure I can implement myself (although it may be interesting to know how particular libraries have solved this problem)
There's a couple of open source data structure that may fit your needs:
CGAL https://www.cgal.org/
OpenMesh http://openmesh.org/
Surface Mesh http://opensource.cit-ec.de/projects/surface_mesh
I've oredered them from the harder to the easier to use. They are all half edge data structures
Take a look at this paper from Bielefeld university (developers of the Surface mesh), I think that it's a good starting point for you!