| 
January 2nd, 2009, 09:56 PM
| | Newbie | | Join Date: Jan 2009
Posts: 3
Country: Thanks: 0
Thanked 0 Times in 0 Posts
| | Simple pre-algebra help needed Okay, this will be pretty elementary, I'm sure. But for some reason, I am having trouble. I am trying to write a program and I am using the following formula:
Offset = Y * 160 + X * 2
This formula works great for what I'm doing. My question is, what is the formula I use to retrieve X and Y, assuming I know Offset. For example, if Offset = 1500, what is the value of X and Y?
If it helps, what this is is X and Y coordinates for video memory. Video memory is broken down into 80x25 characters and each character takes 2 spaces.....So if I want to write to Y=10 and X=5, using my formula above, the offset to write to would be 1610.
So my question is, given 1610 as an Offset, what formula do I use to make Y=10 and X=5?
Hope this makes sense and thanks in advance
Alan
Last edited by mr fantastic; January 2nd, 2009 at 10:30 PM.
Reason: Deleted potentially offensive language
| 
January 2nd, 2009, 10:01 PM
| | Super Member | | Join Date: Dec 2008 Location: Scotland
Posts: 862
Country: Thanks: 6
Thanked 341 Times in 319 Posts
| | Quote:
Originally Posted by icemanind Okay, this will be pretty elementary, I'm sure. But for some reason, I am having trouble. I am trying to write a program and I am using the following formula:
Offset = Y * 160 + X * 2
This formula works great for what I'm doing. My question is, what is the formula I use to retrieve X and Y, assuming I know Offset. For example, if Offset = 1500, what is the value of X and Y?
If it helps, what this is is X and Y coordinates for video memory. Video memory is broken down into 80x25 characters and each character takes 2 spaces.....So if I want to write to Y=10 and X=5, using my formula above, the offset to write to would be 1610.
So my question is, given 1610 as an Offset, what formula do I use to make Y=10 and X=5?
Hope this makes sense and thanks in advance
Alan | So you're asking us to solve an equation with 2 variables? Unfortunately it's not possible to solve for 2 unknowns unless you have 2 equations.
However, if X and Y are restricted to integer values then your equation is a linear diophantine equation (ax+by=c), and these can be solved using the Euclidean Algorithm if c is the greatest common divisor of a and b (or indeed a multiple of the greatest common divisor!) Do you know the algorithm?
Last edited by mr fantastic; January 3rd, 2009 at 03:17 AM.
Reason: Edited quote to match edit of OP
| 
January 2nd, 2009, 10:03 PM
| | Newbie | | Join Date: Jan 2009
Posts: 3
Country: Thanks: 0
Thanked 0 Times in 0 Posts
| | Quote:
Originally Posted by Mush So you're asking us to solve an equation with 2 variables?
Well, if X and Y are restricted to integer values then your equation is a linear diophantine equation, and these can be solved using the Euclidean Algorithm. Do you know the algorithm? |
X and Y are both integer. And no, I don't know that algorithm. Please explain it! | 
January 2nd, 2009, 10:08 PM
| | Super Member | | Join Date: Dec 2008 Location: Scotland
Posts: 862
Country: Thanks: 6
Thanked 341 Times in 319 Posts
| | Quote:
Originally Posted by icemanind X and Y are both integer. And no, I don't know that algorithm. Please explain it! | Extended Euclidean algorithm - Wikipedia, the free encyclopedia
Knock yourself out | 
January 2nd, 2009, 10:17 PM
| | Newbie | | Join Date: Jan 2009
Posts: 3
Country: Thanks: 0
Thanked 0 Times in 0 Posts
| | Quote:
Originally Posted by Mush |
I'm sorry man. The answer to this cannot possibly be this complex. Please let me explain this a different way. Imagine you have a piece of paper. And this piece of paper has 160 squares across and 25 rows of these squares going down. Now lets say you write your letters kind of fat and to write one letter on the piece of paper, it will occupy 2 squares. That means you can have 80x25 letters on this paper. Now lets say we give this paper axises. X goes across and Y goes down. Now lets say I want you to draw a letter at coordinate 10,5. But instead of counting 10 squares across and 5 down, just count each individual square. Remember that 1 letter = 2 squares. So you would count to 1610 and make your letter (cause our formula is Offset(1610)=Y * 160 + X * 5).
Now what I'm trying to do is opposite. What if I said, make a letter at offet 1610. Would you count 1610 spaces? the 1610th space is 10 boxes over and 5 down. Does this make sense? | 
January 2nd, 2009, 10:35 PM
|  | Flow Master | | Join Date: Dec 2007 Location: Zeitgeist
Posts: 12,020
Country: Thanks: 2,514
Thanked 4,695 Times in 4,132 Posts
| | Quote:
Originally Posted by icemanind Okay, this will be pretty elementary, I'm sure. But for some reason, I am having trouble. I am trying to write a program and I am using the following formula:
Offset = Y * 160 + X * 2
This formula works great for what I'm doing. My question is, what is the formula I use to retrieve X and Y, assuming I know Offset. For example, if Offset = 1500, what is the value of X and Y?
If it helps, what this is is X and Y coordinates for video memory. Video memory is broken down into 80x25 characters and each character takes 2 spaces.....So if I want to write to Y=10 and X=5, using my formula above, the offset to write to would be 1610.
So my question is, given 1610 as an Offset, what formula do I use to make Y=10 and X=5?
Hope this makes sense and thanks in advance
Alan | I'm afraid that the answer given by Mush appears to be how your question needs to be answered.
You could always write (or find) a computer program to implement this algorithm.
__________________ There are two things you should never try to prove: the impossible and the obvious. The greater danger for most of us lies not in setting our aim too high and falling short; but in setting our aim too low and achieving our mark. (Michelangelo Buonarroti) To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
| 
January 5th, 2009, 09:28 PM
| | Super Member | | Join Date: Dec 2008 Location: Scotland
Posts: 862
Country: Thanks: 6
Thanked 341 Times in 319 Posts
| | It might be interesting to note, OP, that even if there was a simpler method for finding X and Y in your equation, that there is not one unique solution. There are in fact INFINITELY many solutions. You may use the regular euclidian algorithm to find one solution.
If you use the regular algorithm, and get a solution(x,y). Then there are other solutions of the form  where k is a member of the integers. There are in fact infinitely many integers, and hence there are infinitely many solutions so such an equation.
Hence, even if there was a simpler method, it might take you infinitely many recursions until you got to your desired answer. In your example: (y=10,x=5). Although I do realise that your grid limits the solutions such that the integers are confined to your grid, so perhaps it's not such an impractical endeavour.
If you would like further explanation on how to solve you problem using the regular euclidian algorithm, then just ask. | 
January 6th, 2009, 05:39 AM
| | MHF Contributor | | Join Date: Apr 2005
Posts: 3,301
Thanks: 291
Thanked 1,161 Times in 1,063 Posts
| | Quite frankly, the simplest way to write a program to find integers X and Y such that Offset = Y * 160 + X * 2 for a given "offset" is first to write X= (Offset- 160Y)/2 and then step through integer values for Y until you find one that gives X integer also. | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -7. The time now is 11:41 PM. | | |