C
C#2y ago
Natro

❔ ✅ Applying math to programming

Hi, this is not necessarily a C# question and more question towards math and programming. I am using C# as my language so I hope it's okay to ask this here. I have always been struggling with applying math equations and so on to programming and I am finally tackling this mental block I have and trying to force myself to learn more. I have a 2 dimensional array (grid) of 0 or 1 (which size is set dynamically) I would like to use x^4 + y^4 = 1 to fill it out. https://www.wolframalpha.com/input?i=x%5E4+%2B+y%5E4+%3D+1 How can I draw this shape in it? I would also like to go beyond and how could I fill it out or reverse fill it out in my grid? Just a small pointer on how to use math to achieve some different result would help help so much. Thank you. Example (With bigger size edges would be smoother):
1 1 1 1 1 1 1 1 1 1
1 1 0 0 0 0 0 0 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 0 0 0 0 0 0 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 0 1 1
1 1 1 1 1 1 1 1 1 1
8 Replies
ACiDCA7
ACiDCA72y ago
i hope i understood the question correctly and will give it a try so thats a 10x10 grid asuming the zeropoint is exactly in the middle and goes from -1 to 1 it would mean every point is 0.2 away from the next so now loop from -1 to 1 in 0.2 increments and set x to that value, then solve for y set the closest to index to true... that would be a very naive approach
Natro
Natro2y ago
Thank you very much, helped a bit to understand how to approach something like this 🙂
D.Mentia
D.Mentia2y ago
You have someArray[,]
for(int x = 0; x < someArray.GetLength(0); x++)
for(int y = 0; y < someArray.GetLength(1); y++)
someArray[x,y] = SolveFunction(x-someArray.GetLength(0)/2,y-someArray.GetLength(1)/2);
for(int x = 0; x < someArray.GetLength(0); x++)
for(int y = 0; y < someArray.GetLength(1); y++)
someArray[x,y] = SolveFunction(x-someArray.GetLength(0)/2,y-someArray.GetLength(1)/2);
Not being a math person, I don't know how SolveFunction is supposed to work. But the thing I'm trying to point out is that your array already has x and y, and just subtracting half of the size gets the center to 0 (or near it) for graphing purposes Judging by your example, SolveFunction is probably just something like
if (Math.Pow(x,4)+Math.Pow(y,4) == 1)
return 1;
return 0;
if (Math.Pow(x,4)+Math.Pow(y,4) == 1)
return 1;
return 0;
And so you'll have to make sure the array is an appropriate size to be able to have that ever be true, or apply some scaling factor to it
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
ChucklesTheBeard
The previous suggestions seem to assume that the function will be exactly equal at some "pixels"; that will almost never be the case for any nontrivial formula. You may instead want to calculate a bunch of (x, y) points that lie exactly on the curve, for your example function perhaps converting the function to its polar form then iterating through theta would be a good start. Then for each point, you could round to the nearest "pixel coordinate" to set that cell's value.
Auger
Auger2y ago
@patrickk is this a bug in Accord?
Patrick
Patrick2y ago
Other than the title not removing the solved checkmark no It was closed, then reopened because of a message and then never closed
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.