### Author Topic: Angle Calculation Help  (Read 2020 times)

0 Members and 1 Guest are viewing this topic.

#### ACagliano

• Posts: 900
• Rating: +31/-2
##### Angle Calculation Help
« on: October 28, 2011, 09:41:30 pm »
Also posted on cemetech...

I had an old program that I made in TI-Basic. I have wanted to convert it into z80 for a while. It was a Lewis Structures drawing program. It several shortcomings: (1) it could not handle drawing off-screen, and (2) It could only handle formulas with one central atom, and the others around it. (NH4, for instance).

I want to redo this. In z80. Such that it can not only handle such formulas as HH4 and H2O but more complex ones as well, and shows single, double, and triple bonds. So, I'm asking the following questions:

1. How do I calculate angle degrees in z80? When drawing Lewis Structures, spare electrons must be accounted for by leaving one space. For instance, in a water molecule, you know that it is a bent molecule. Because of the electrons left unbonded on the hydrogen, the oxygen molecules are pushed to the opposite side, at 90 degree angles to each other. Understand what I'm asking? Or maybe someone who knows more about this can explain better?

2. How do I make the program able to scroll to the left or right of the display in order to show offscreen parts? Would the DCS GUI features be best for this?

Any help I could get would be great. I'd be willing to co-author the project with someone who knows how draw Lewis Structures , who could handle the technicalities of calculating and drawing.

#### thepenguin77

• z80 Assembly Master
• LV10 31337 u53r (Next: 2000)
• Posts: 1587
• Rating: +823/-5
• The game in my avatar is bit.ly/p0zPWu
##### Re: Angle Calculation Help
« Reply #1 on: October 28, 2011, 11:20:41 pm »
Well, before you start worrying about that stuff, I would think you need to get a solid format going for how you represent your molecules in memory. I would say that your data structures for each of your atoms should look like this.

[Atomic number][lone pairs][charge][flags xx112234][bonded id 1][bonded id 2][bonded id 3][bonded id 4][bonded id 5][bonded id 6][display x][display y]

Where:
- atomic number = atomic number
- lone pairs = number of lone pairs
- charge = formal charge
- flags = bond type 0 = single bond, 1 = double bond, 2 = triple bond, 3 = ionic
- bonded id # = the array position of the other atom it is bonded to
- display x/y = where it is displayed on the full buffer

So with this system, you have the 10 byte atom structures lined up in memory. Each one would have an ID based on where it is in memory as well as the atoms it is bonded to. The goal would then be to parse what the user typed and turn it into one of these structures. If you start to get triple bonds, you may have to switch some of the bond id's around as you add them, but that is fine.

Using this system, you should be able to represent any kind of atom I can think of, even complex organic molecules.

1. This one is easy, assuming you don't start getting really space filling molecules (2,2 - dimethylpropane), the angle calculation is as simple as taking 360 divided by the number of atoms and lone pairs bonded to the atom. So, if it is bonded to 2 atoms and has one lone pair, 360/3 = 120. Then you'll just have to make a sine and cosine LUT to figure out how to draw your lines. (The LUT only needs 360, 180, 120, 90, 72, and 60)

2. Scrolling is a bit harder, any way you do it it's going to be messy. I have two approaches, the oversized buffer method, and the clipping method.
• Oversized buffer: For this, you'll want to allocate a big buffer, 150x150 pixels should be fine. Then, you'll have to make your own drawing methods to draw to this buffer. You really only have to copy over a line drawer and a vPutS, which isn't really that tough. After that, you simply draw you whole molecule to the buffer and only display the 96x64 region that is currently showing.
• Clipping method: This method might be harder, but it uses fewer resources. You will probably be able to use the system vPutS for this but you'll need a special line routine that automatically clips itself to the edges of the screen. (badja made one). From here, you also need to define how far the screen has scrolled from the top left corner of the true screen. The final step is just to take the coordinates of something on the screen, subtract from it how much the screen has shifted, and draw the atom to the resulting position on the screen. You'll just have to watch out to make sure that you don't accidentally try to vPutS something off the screen.
zStart v1.3.013 9-20-2013
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
-Runer112

#### ACagliano

• Posts: 900
• Rating: +31/-2
##### Re: Angle Calculation Help
« Reply #2 on: October 29, 2011, 10:54:02 am »
ok. I'll see what I can do. Question. For "atomic number", can't I just use the number of valence electrons? I think that, when it comes to drawing Lewis Structures, only the valance orbital is what affects bonding.

I may need help with rendering the final object to the screen.

Also, how do you determine what molecules make up the main chain, and what molecules go around the main chain?
« Last Edit: October 29, 2011, 11:14:01 am by ACagliano »

#### thepenguin77

• z80 Assembly Master
• LV10 31337 u53r (Next: 2000)
• Posts: 1587
• Rating: +823/-5
• The game in my avatar is bit.ly/p0zPWu
##### Re: Angle Calculation Help
« Reply #3 on: October 29, 2011, 12:42:26 pm »
ok. I'll see what I can do. Question. For "atomic number", can't I just use the number of valence electrons? I think that, when it comes to drawing Lewis Structures, only the valance orbital is what affects bonding.

Well, yeah, you could do that, but then you wouldn't know what letter to display . It depends how far you want this program to go, but you might have to look farther than valence electrons if you are going to handle expanded octets.

Quote
I may need help with rendering the final object to the screen.

Ok

Quote
Also, how do you determine what molecules make up the main chain, and what molecules go around the main chain?

That is where the real challenge comes in. For inorganic stuff, you might almost need to have a database of polyatomic ions. For your purposes, you probably shouldn't end up having more than 50. If you don't want to do that You could also try to determine the structure of the polyatomic ion by comparing the number of valance electrons of each atom along with how many of each atom is present. Typically, the element with the most atoms will be the outer atoms with oxygen almost always being an outside atom. (I can think of a few weird ones, CN-, SCN-, and OCl-. Also, stuff like NO3- has a double bond in it.)

For organic materials, once you figure out a system, it shouldn't really be that hard. Just try to determine what structures are side chains and what are main chains and you should be ok. If you really love chemistry, you could try to parse IUPAC names, but that would be tough.
zStart v1.3.013 9-20-2013
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
-Runer112

#### ACagliano

• Posts: 900
• Rating: +31/-2
##### Re: Angle Calculation Help
« Reply #4 on: October 30, 2011, 11:49:42 am »
ok. I'll see what I can do. Question. For "atomic number", can't I just use the number of valence electrons? I think that, when it comes to drawing Lewis Structures, only the valance orbital is what affects bonding.

Well, yeah, you could do that, but then you wouldn't know what letter to display . It depends how far you want this program to go, but you might have to look farther than valence electrons if you are going to handle expanded octets.

Quote
I may need help with rendering the final object to the screen.

Ok

Quote
Also, how do you determine what molecules make up the main chain, and what molecules go around the main chain?

That is where the real challenge comes in. For inorganic stuff, you might almost need to have a database of polyatomic ions. For your purposes, you probably shouldn't end up having more than 50. If you don't want to do that You could also try to determine the structure of the polyatomic ion by comparing the number of valance electrons of each atom along with how many of each atom is present. Typically, the element with the most atoms will be the outer atoms with oxygen almost always being an outside atom. (I can think of a few weird ones, CN-, SCN-, and OCl-. Also, stuff like NO3- has a double bond in it.)

For organic materials, once you figure out a system, it shouldn't really be that hard. Just try to determine what structures are side chains and what are main chains and you should be ok. If you really love chemistry, you could try to parse IUPAC names, but that would be tough.

I can do the chart of polyatomics, so that first, the program compares the input with the polyatomics chart. Secondly, I found two rules to use...(1) the atoms with the highest number of valence electrons are usually the central atoms, and (2) the atoms that are the least electronegative are usually the central atoms. I'm currently looking for a computer algorithm to determine the central atoms, but I doubt I'll find one.
« Last Edit: October 30, 2011, 12:00:03 pm by ACagliano »

#### thepenguin77

• z80 Assembly Master
• LV10 31337 u53r (Next: 2000)
• Posts: 1587
• Rating: +823/-5
• The game in my avatar is bit.ly/p0zPWu
##### Re: Angle Calculation Help
« Reply #5 on: October 31, 2011, 12:30:54 am »
Well, I think there's a bit more than valence electrons, especially considering this pattern: SiO4-4, PO4-3, SO4-2, ClO4-. But aside from oxygen, I think valence electrons are the way to do it.
zStart v1.3.013 9-20-2013
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
-Runer112

#### AngelFish

• Is this my custom title?
• LV12 Extreme Poster (Next: 5000)
• Posts: 3240
• Rating: +269/-27
• I'm a Fishbot
##### Re: Angle Calculation Help
« Reply #6 on: October 31, 2011, 02:31:49 am »

2. Scrolling is a bit harder, any way you do it it's going to be messy. I have two approaches, the oversized buffer method, and the clipping method.
• Oversized buffer: For this, you'll want to allocate a big buffer, 150x150 pixels should be fine. Then, you'll have to make your own drawing methods to draw to this buffer. You really only have to copy over a line drawer and a vPutS, which isn't really that tough. After that, you simply draw you whole molecule to the buffer and only display the 96x64 region that is currently showing.
• Clipping method: This method might be harder, but it uses fewer resources. You will probably be able to use the system vPutS for this but you'll need a special line routine that automatically clips itself to the edges of the screen. (badja made one). From here, you also need to define how far the screen has scrolled from the top left corner of the true screen. The final step is just to take the coordinates of something on the screen, subtract from it how much the screen has shifted, and draw the atom to the resulting position on the screen. You'll just have to watch out to make sure that you don't accidentally try to vPutS something off the screen.
I would highly recommend not going with the virtual buffer method. As you mentioned, it's messy. It's also rather inefficient for long carbon/Silicon chains where you have large 2d plots, but relatively few molecules. I'd instead recommend a more "vector"-ish approach where the coordinates of the nuclear centers are represented in a virtual 3d space as a table of coordinates and bond relations. When you need to display them, you define a 2d buffer that will be directly translated to the screen and just project the points from that 3d space into the buffer along with the bonds. A bit more complex, perhaps, but it allows "infinite resolution" of arbitrarily large molecules (within reason of course) and uses a less memory for molecules with low nuclear density since all you're storing is an extra coordinate over the 2d buffer method.

As for generating the structures themselves from the formulas, I'll be blunt. It's basically impossible to do at any significant level of accuracy, since the structure of a molecule depends on a lot more than just an enumeration of the atoms in it. For example, it often depends on the prior history of its constituent atoms. A good demonstration of this is Barrelene, a molecule with the formula C8H8. This molecular formula is also shared by Cubane and Styrene, among a few others. These isomers are all [somewhat] stable in their radically different configurations and determining which one the user means is nigh impossible. A structural formula like SMILES is much less ambiguous, pretty widely available for most molecules, and essentially eliminates the near impossible problem of predicting molecular structure.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

#### ACagliano

• Posts: 900
• Rating: +31/-2
##### Re: Angle Calculation Help
« Reply #7 on: October 31, 2011, 08:59:39 am »

2. Scrolling is a bit harder, any way you do it it's going to be messy. I have two approaches, the oversized buffer method, and the clipping method.
• Oversized buffer: For this, you'll want to allocate a big buffer, 150x150 pixels should be fine. Then, you'll have to make your own drawing methods to draw to this buffer. You really only have to copy over a line drawer and a vPutS, which isn't really that tough. After that, you simply draw you whole molecule to the buffer and only display the 96x64 region that is currently showing.
• Clipping method: This method might be harder, but it uses fewer resources. You will probably be able to use the system vPutS for this but you'll need a special line routine that automatically clips itself to the edges of the screen. (badja made one). From here, you also need to define how far the screen has scrolled from the top left corner of the true screen. The final step is just to take the coordinates of something on the screen, subtract from it how much the screen has shifted, and draw the atom to the resulting position on the screen. You'll just have to watch out to make sure that you don't accidentally try to vPutS something off the screen.
I would highly recommend not going with the virtual buffer method. As you mentioned, it's messy. It's also rather inefficient for long carbon/Silicon chains where you have large 2d plots, but relatively few molecules. I'd instead recommend a more "vector"-ish approach where the coordinates of the nuclear centers are represented in a virtual 3d space as a table of coordinates and bond relations. When you need to display them, you define a 2d buffer that will be directly translated to the screen and just project the points from that 3d space into the buffer along with the bonds. A bit more complex, perhaps, but it allows "infinite resolution" of arbitrarily large molecules (within reason of course) and uses a less memory for molecules with low nuclear density since all you're storing is an extra coordinate over the 2d buffer method.

As for generating the structures themselves from the formulas, I'll be blunt. It's basically impossible to do at any significant level of accuracy, since the structure of a molecule depends on a lot more than just an enumeration of the atoms in it. For example, it often depends on the prior history of its constituent atoms. A good demonstration of this is Barrelene, a molecule with the formula C8H8. This molecular formula is also shared by Cubane and Styrene, among a few others. These isomers are all [somewhat] stable in their radically different configurations and determining which one the user means is nigh impossible. A structural formula like SMILES is much less ambiguous, pretty widely available for most molecules, and essentially eliminates the near impossible problem of predicting molecular structure.

Hmmm. I think I know what you are talking about, but I will not be able to do a 3d rendering on my own.

As for the structures, I'm just interested in a general drawing, not with prior conditions and stuff. Just the simple textbook way we learned to draw Lewis Structures. What does SMILES mean again?

#### aeTIos

• You got stair'd!
• LV12 Extreme Poster (Next: 5000)
• Posts: 3912
• Rating: +184/-32
• I luv birdies!
##### Re: Angle Calculation Help
« Reply #8 on: October 31, 2011, 09:24:29 am »
sounds promising )
Correct me if I'm wrong.

dagaem

#### AngelFish

• Is this my custom title?
• LV12 Extreme Poster (Next: 5000)
• Posts: 3240
• Rating: +269/-27
• I'm a Fishbot
##### Re: Angle Calculation Help
« Reply #9 on: October 31, 2011, 12:40:48 pm »
SMILES is an acronym for *Googles* "Simplified molecular input line entry specification." Basically, it's a way to represent molecular structures in ASCII that happens to enjoy wide support by pretty much every professional tool out there.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

#### ACagliano

• Posts: 900
• Rating: +31/-2
##### Re: Angle Calculation Help
« Reply #10 on: November 03, 2011, 10:43:55 am »

[Atomic number][lone pairs][charge][flags xx112234][bonded id 1][bonded id 2][bonded id 3][bonded id 4][bonded id 5][bonded id 6][display x][display y]

Where:
- atomic number = atomic number
- lone pairs = number of lone pairs
- charge = formal charge
- flags = bond type 0 = single bond, 1 = double bond, 2 = triple bond, 3 = ionic
- bonded id # = the array position of the other atom it is bonded to
- display x/y = where it is displayed on the full buffer

So with this system, you have the 10 byte atom structures lined up in memory. Each one would have an ID based on where it is in memory as well as the atoms it is bonded to. The goal would then be to parse what the user typed and turn it into one of these structures. If you start to get triple bonds, you may have to switch some of the bond id's around as you add them, but that is fine.

Using this system, you should be able to represent any kind of atom I can think of, even complex organic molecules.

One problem...the flags.

A flag can only be 0 or 1, but i'd also need 2 or 3 as a value. How is that gonna happen?

#### thepenguin77

• z80 Assembly Master
• LV10 31337 u53r (Next: 2000)
• Posts: 1587
• Rating: +823/-5
• The game in my avatar is bit.ly/p0zPWu
##### Re: Angle Calculation Help
« Reply #11 on: November 03, 2011, 03:19:05 pm »

One problem...the flags.

A flag can only be 0 or 1, but i'd also need 2 or 3 as a value. How is that gonna happen?

Allocate two bits for it. That's why I wrote xx112234. I gave atoms 1 and 2 two bits because they could both potentially ionic or triple bonded (NaCN). Atoms 3 and 4 only need 1 bit though because you can throw any ionic bonds or triple bonds into the first two places. Although, I guess technically you could have 3 ionic bonds. So maybe you should just number them 11223344 and put the atoms wherever you want. Also, if you get an atoms 5 and 6, you can just assume that they are single bonded.

Some samples: (I'm only putting the central atom on here)

[Atomic number][lone pairs][charge][flags 11223344][bonded id 1][bonded id 2][bonded id 3][bonded id 4][bonded id 5][bonded id 6]

NaCN
[6][0][-1][10 11 00 00][Na atom ID][N atom ID][0][0][0][0]

CO2
[6][0][0][01 01 00 00][H atom ID][Other H atom ID][0][0][0][0]

SF6
[16][0][0][00 00 00 00][S atom ID][Other S atom ID][Other S atom ID][Other S atom ID][Other S atom ID][Other S atom ID]

H2O
[8][2][0][00 00 00 00][H atom ID][Other H atom ID][0][0][0][0]

NO3-
[7][0][1][01 00 00 00][Double bonded O ID][Other O ID][Other O ID][0][0][0]
zStart v1.3.013 9-20-2013
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
-Runer112

#### ACagliano

• Posts: 900
• Rating: +31/-2
##### Re: Angle Calculation Help
« Reply #12 on: November 03, 2011, 07:57:27 pm »
Hmmm. Why not just two bytes, and have four bits out the second allocated to 5 and 6, just for the sake of making it easier on me. And, can you not have 7 or 8 atoms bonded (like 8 H's). Now, as for bonded id, could I do, instead of the number of the array it is bonded to, a pointer to that atom's data? That way, I can point to another atom if needed, or if bonded to a polyatomic, point to that polyatomic's entry in the "polyatomics table"

#### thepenguin77

• z80 Assembly Master
• LV10 31337 u53r (Next: 2000)
• Posts: 1587
• Rating: +823/-5
• The game in my avatar is bit.ly/p0zPWu
##### Re: Angle Calculation Help
« Reply #13 on: November 03, 2011, 10:20:57 pm »
No, 6 is the max, as long as you don't go into crazy stuff which I'm not even sure is possible. (6 atoms involves hybridizing the s, p, and d orbitals, to do 8, you might have to start messing with the f orbital.) Also, you have to remember that atoms share electrons, so, for normal cases, you'll only have 4 atoms bonded.

You could do 2 bytes for flags, but to be honest, I don't think you'll ever run into a situation where you'll need 5 and 6 to have flags. Atoms can normally only have 4 bonds, so that is not a problem. The only time you start messing with the 5th and 6th atoms are when you go into expanded octets. The only possible way that your 5th atom could have a flag would be if it were double bonded, and I'm not sure this ever happens. You would have something along the lines of SF4O, which is not a real compound, and the O would be double bonded to the S. I don't think this is something you need to worry about though.

I don't think you understand what I meant by the ID. Lets say we're doing KNO3: Here's what your full data set would look like.

1. K [19][0][+1][11 00 00 00][2][0][0][0][0][0]
2. O [8][3][-1][11 00 00 00][1][3][0][0][0][0]
3. N [7][0][+1][01 00 00 00][4][2][5][0][0][0]
4. O [8][2][0][01 00 00 00][3][0][0][0][0][0]
5. O [8][3][-1][00 00 00 00][3][0][0][0][0][0]

The ID's are essentially pointers to the data. The only thing that makes it nicer is that it is 1 byte rather than two. Of course, you could do addresses if you really wanted, it would just take up more space.

Pointing to a polyatomic isn't a bad idea actually. Though, you'd save yourself a headache if you manage to parse the polyatomics on the fly. The only problem with pointing to polyatomics would be difficulties with drawing them. You'd have to special case them because you'd have a single polyatomic ion that might be bonded to multiple things. Cu(OH)2.
zStart v1.3.013 9-20-2013
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
-Runer112

#### ACagliano

• Posts: 900
• Rating: +31/-2
##### Re: Angle Calculation Help
« Reply #14 on: November 04, 2011, 09:11:50 am »
No, 6 is the max, as long as you don't go into crazy stuff which I'm not even sure is possible. (6 atoms involves hybridizing the s, p, and d orbitals, to do 8, you might have to start messing with the f orbital.) Also, you have to remember that atoms share electrons, so, for normal cases, you'll only have 4 atoms bonded.

You could do 2 bytes for flags, but to be honest, I don't think you'll ever run into a situation where you'll need 5 and 6 to have flags. Atoms can normally only have 4 bonds, so that is not a problem. The only time you start messing with the 5th and 6th atoms are when you go into expanded octets. The only possible way that your 5th atom could have a flag would be if it were double bonded, and I'm not sure this ever happens. You would have something along the lines of SF4O, which is not a real compound, and the O would be double bonded to the S. I don't think this is something you need to worry about though.

I don't think you understand what I meant by the ID. Lets say we're doing KNO3: Here's what your full data set would look like.

1. K [19][0][+1][11 00 00 00][2][0][0][0][0][0]
2. O [8][3][-1][11 00 00 00][1][3][0][0][0][0]
3. N [7][0][+1][01 00 00 00][4][2][5][0][0][0]
4. O [8][2][0][01 00 00 00][3][0][0][0][0][0]
5. O [8][3][-1][00 00 00 00][3][0][0][0][0][0]

The ID's are essentially pointers to the data. The only thing that makes it nicer is that it is 1 byte rather than two. Of course, you could do addresses if you really wanted, it would just take up more space.

Pointing to a polyatomic isn't a bad idea actually. Though, you'd save yourself a headache if you manage to parse the polyatomics on the fly. The only problem with pointing to polyatomics would be difficulties with drawing them. You'd have to special case them because you'd have a single polyatomic ion that might be bonded to multiple things. Cu(OH)2.

Well, you have more experience with this stuff, so I'll go with whatever you think is best.