# Geoff the Medio

Member

322

220 Neutral

• Rank
Member
1. ## "averaging" several vectors

You might be overlooking the case where you add up two vectors (or more) and the result is a null vector.
2. ## Python exercise problem

The y-intercept is the point where the line crosses the y-axis. The y-axis is defined by x = 0, so you need to find what the value of the equation y = m*x + b is when x = 0... which is y = b. The equation y = m*x + b has four parameters in it, so if you have three of them, you can solve for the last. If you have a point on the line, you have a matched pair of y and x, and if you have the slope, you have m, so can solve for b. Generalize the method you use to solve for b, and write the function to do the equivalent.
3. ## error C2075-wont go away

Make an array big enough to hold whatever you want to push onto your stack, and keep track of where the "top" of the stack is with an array index stored in a separate variable. When pushing or popping from the stack, adjust the index appropriately.
4. ## Variable Holding Class Instance [C++]

Quote:Original post by timmeh_1250 Basically, I want to be able to assign a room to a variable, so a variable can be used to track the current room, and additional class variables in each room can store the names of the rooms adjacent to it. I want to store the room in a variable, so that if I were to create a room ("Room home;", for example) I could store it in a variable [...] or at least store the address of the rooms as pointers... If you're happy storing the address of the rooms as pointers, why not use a pointer to room? Room* current_room = &home

6. ## standard deviation

One pass that does twice the work per piece of data isn't much better than two passes that do half the work, is it? However, if you have a large list of data that's randomly distributed, so that there is no tendency for high or low values to appear at the start more so than later, or values closer or further from the average to appear at the start, you could estimate the standard deviation using the usual or "single-pass" calculation, but only using part of the data. Using more data gives a better estimate, but the improvement for each additional sample decreases as the number of samples increases.
7. ## Spherical gaussian

Quote:Original post by Zarathustra So the line in the paper I'm implementing says the following: "Each level of the mip-map is computed by convolving the environment map with a spherical Gaussian that has twice the standard deviation of the previous level" So my question here is, what is a spherical gaussian? I need some kind of formulation in function of theta and phi. The angular component is probably constant. That is, there is no dependence on angles "theta" and "phi". Rather, the spherical gaussian is probably defined F(r, theta, phi) = A*exp(-r^2/sigma^2) where r is radial distance.
8. ## [C++] Finding characters in a string

If you've got more than a single-character input, consider string::find http://www.cplusplus.com/reference/string/string/find.html size_t find ( const string& str, size_t pos = 0 ) const You'd use a loop, in which you call find on your string with pos initially 0. After each find, update pos to the output of find + 1 (one past the start of the previous match), and repeat. After you stop getting matches, exit the loop. Keep track of how many matches you get, and you've counted the instances of the string str in your main string.
9. ## Getting random points within an area:

If you can't understand the math for the more complicated cases, you probably won't be able to program a solution to all the cases listed in your original post. You might want to start with something much simpler, like a picking points on a 1D range with a simple probability distribution. The simplest case: You want a point at position x which ranges continuously between 0 and 1 in one dimension. The point should be chosen according to a uniform distribution (all possible x have equal probability). If you have a random number generating function rand() that outputs a number between 0 and 1, you can immediately generate points for this case. If you want a more complicated probability distribution, you need to parameterize it in terms of the position. We'll call the probability distribution function P(x). P(x) gives the probability density at the position x. This is a somewhat abstract concept, as P(x) is not the probability that a chosen point will have value x. Rather, if you integrate P(x) from x1 to x2, you get the probability that a chosen point will lie between x1 and x2. Probability distributions like P(x) should be normalized, meaning that the total probability of a point being between 0 and 1 should be 100%. Mathematically, that means that integrating P(x) from 0 to 1 with respect to x gives 1. If you have a function P(x) defined on the range 0 to 1, you can use two methods to generate points on the range. The simpler one is the second I described earlier. Randomly pick a point y in the range 0 to 1 using a uniform distribution (which is discussed above), and calculate P(y). Then generate a random value z with a uniform distribution between 0 and the largest value your function P(x) takes for any values of x between 0 and 1 (the range on which it is defined). If this second random value z is larger than P(y), then reject y and repeat for a new value of y until you get one that isn't rejected. When you get a non-rejected y, you can use it as a random point in the range 0 to 1 that was generated according to the probability distribution P(x).
10. ## Getting random points within an area:

There are a few parts to this that are significant problems on their own... If you have a probability distribution P(x) that's normalized for some range of x values, say x = x1 .. x2, then the integral of P(x)dx from x = x1 to x2 is 1. That is, the total probability of the chosen point being in the range x1 to x2 is 1. To randomly pick a point in the range x1 to x2 from that probability distribution, you need to sample the inverted cumulative distribution function corresponding to P(x). This is commonly used in monte carlo simulations of physical processes (among others). If you integrate P(x) from x1 to x, you get C(x), the cumulative distribution function. C(x1) = 0, and C(x2) = 1 due ot the normalization condition above. If you invert C(x), you get the inverted cumulative distribution function, which I'll call x = Q(c). If you randomly generate a number c between 0 and 1 with a uniform (what you called linear) distribution, and then caluclate Q(c), you'll get an x value in the range x1 to x2, which will have effectively sampled P(x). You might want to read about monte carlo, and Quantile functions (what wikipedia seems to call the inverted cumulative distribution). Once you've got 1D sampling on a range worked out, you can parameterize your 2D surfaces (shape) so that a pair of coordinates (a, b) that are both independently uniformly sampled in the range 0 to 1 gives you a point on the surface that's also uniformly distributed. This is a whole separate problem for complicated shapes, though is easy for lines or rectangles. Your distributions (gaussians, uniform) are rotationally symmetric, so you can probably parameterize the non-simple ones in terms of angular coordinate (uniformly sampled) and radial coordinate, which will have to be sampled carefully to get uniform sampling across the surface. Then you can invert the radial part, find the inverse cumulative distribution function, and sample it to get a radial position... But then it might be more difficult if you want to use a rotationally symetric distribution on a non-circular surface, as normalization will be quite tricky, and possible range of radial value will vary with angle... You might have an easier time if you sample a 1D gaussian in two dimensions, instead of doing a true rotationally symmetric 2D gaussian distribution for the non-circular shapes. Edit: also, integrating and inverting gaussians isn't going to be doable in closed form, so you'll have to numerically approximate that. Another alternative is to define the probability distribution you want in terms of (x, y) on the surface. Randomly pick (using a uniform distribution) a point on the surface. Find the maximum value of the probability distribution, and randomly sample a uniform value in the range from 0 to the distribution maximum. If the randomly sampled value is larger than the probability distribution at (x, y), reject (x, y), and generate a new point on the surface, and repeat. This will require repeated iterations in some cases to get a point, particularly for very peaky distributions, but is probably a lot easier to implement. /Edit This has gotten a bit rambly and probably hard to follow, but hopefully contains some useful thoughts and suggestions...
11. ## Redirecting Python stderr and stdout to c++ functions

I didn't really assume redirected output wouldn't be longer than 1000 characters, but rather imposed the limit intentionally, in case a bad pointer was passed to a chunk of memory with no null characters for a long while, or other malicious or accidental misuse. I figured 1000 characters was "long enough" for most realisitic use cases. Not writing output before the next newline isn't a problem, because the code I'm actually writing doesn't just redirect stderr to stdout. The posted code was a simplified example. The real code captures both stderr and stdout and redirects them to a logging system that outputs to a file. There's no way for anyone to see this output interactively while using the exposed library, so I don't see a need to ensure partial-lines are output as soon as possible... and I really don't want to output a partial line anyway, because the logger takes text one line at a time, and wouldn't handle partial lines properly. I forgot to include the declaration of std::string buffer in the previous source block, but it's purpose is to accumulate the null-terminated strings that Python outputs until an endline is reach, at which point the concatenated string is sent to the logger. The issue about assuming only one newline per log string is valid though... I guess to handle this properly, I should stick the outputting line into the loop, instead of using a flush flag... static const int MAX_SINGLE_CHUNK_TEXT_SIZE = 1000; static std::string log_buffer(""); void LogText(const char* text) { for (int i = 0; i < MAX_SINGLE_CHUNK_TEXT_SIZE; ++i) { if (text == '\0') break; if (text == '\n' || i == MAX_SINGLE_CHUNK_TEXT_SIZE - 1) { AIInterface::LogOutput(log_buffer); log_buffer = ""; } else { log_buffer += text; } } }
12. ## Redirecting Python stderr and stdout to c++ functions

That seems to have been my problem. I worked around it by some kludgy-seeming scanning of the input: void happy_write(const char* text) { bool flush = false; for (int i = 0; i < 1000; ++i) { if (text == '\0') break; if (text == '\n') flush = true; buffer += text; } if (flush) { std::cout << "captured stderr text:" << buffer; buffer = ""; } } Which seems to work, but also seems like it's probably a bad idea, and that there's an obvious-but-not-to-me better way. Anyway, thanks.
13. ## Unity Redirecting Python stderr and stdout to c++ functions

Similar to what was discussed here, I'm trying to redirect python stderr and stdout so that I can get the text of python error messages in the cpp code that's calling the embedded python script. I've been somewhat successful, but am getting some weird results with extra endlines. To simplify the problem, I've made a library using Boost Python that can be imported into Python that does similar redirection of stderr... Some cpp code to expose a simple "logging" library to Python: void happy_write(const char* text) { std::cout << "captured stderr text:"<< text; } BOOST_PYTHON_MODULE(Logger) { boost::python::def("write", happy_write); } and then, in the python interpreter: >>> import Logger >>> import sys >>> sys.stderr = Logger >>> blah captured stderr text:Traceback (most recent call last): captured stderr text: File "<stdin>", line 1, in <module> captured stderr text:NameErrorcaptured stderr text:: captured stderr text:name 'blah' is not definedcaptured stderr text: >>> For some reason, I get "captured stderr text:" on stdout twice for each line of text sent from Python to stderr, with the second copy being seemingly randomly inserted into the Python error message text. Any ideas why this might be? Thanks.
14. ## My spring based system blows up :(

What integration scheme are you using? You may need to change to one that's more numerically stable...
15. ## All points in a triangle are not on a single plane!?

v0, v1 and v2 are vectors, not points (or rather, they should be). You need to subtract pairs of points to get vectors that lie in the plane. By using the points in the plane as vectors, you've essentially used vectors from (0, 0, 0) to the points on the plane. Since (0, 0, 0) is not on the plane, these vectors are not parallel to the plane, and their cross product is not the normal. A sanity check: Your triangle points all have z = 2, so the plane is parallel to the X-Y plane, and the normal to the plane should be some multiple of (0, 0, 1).