{"id":284,"date":"2025-09-10T06:20:25","date_gmt":"2025-09-10T06:20:25","guid":{"rendered":"https:\/\/learn-by-animation.com\/?page_id=284"},"modified":"2025-09-10T06:45:09","modified_gmt":"2025-09-10T06:45:09","slug":"visualising-crystal-lattice","status":"publish","type":"page","link":"https:\/\/learn-by-animation.com\/?page_id=284","title":{"rendered":"Visualising Crystal Lattice"},"content":{"rendered":"\n<div class=\"lattice-widget\">\n  <div id=\"viewer\"><\/div>\n  <div id=\"controls\">\n    <label for=\"radiusSlider\">Atomic Radius (r): <\/label>\n    <input type=\"range\" id=\"radiusSlider\" min=\"0.1\" max=\"2\" step=\"0.01\" value=\"1\">\n    <span id=\"rValue\">1.00<\/span>\n    <br>\n    <button onclick=\"setLattice('sc')\">Simple Cubic<\/button>\n    <button onclick=\"setLattice('bcc')\">Body-Centered Cubic<\/button>\n    <button onclick=\"setLattice('fcc')\">Face-Centered Cubic<\/button>\n  <\/div>\n  <div id=\"formula\"><\/div>\n<\/div>\n\n<style>\n.lattice-widget #viewer {\n  width: 100%;\n  max-width: 600px;\n  height: 400px;\n  margin: 20px auto;\n  border: 1px solid #ccc;\n  border-radius: 8px;\n}\n.lattice-widget #viewer canvas {\n  position: relative !important;\n}\n.lattice-widget #controls, \n.lattice-widget #formula {\n  max-width: 600px;\n  margin: 10px auto;\n  text-align: center;\n}\n.lattice-widget button {\n  margin: 5px;\n  padding: 5px 10px;\n  cursor: pointer;\n}\n.lattice-widget label {\n  font-weight: bold;\n}\n<\/style>\n\n<script src=\"https:\/\/3dmol.csb.pitt.edu\/build\/3Dmol-min.js\"><\/script>\n<script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/mathjax\/2.7.7\/MathJax.js?config=TeX-MML-AM_CHTML\"><\/script>\n\n<script>\ndocument.addEventListener(\"DOMContentLoaded\", function() {\n  const container = document.querySelector(\".lattice-widget\");\n  let viewer = $3Dmol.createViewer(container.querySelector(\"#viewer\"), {backgroundColor: 'white'});\n  let latticeType = 'sc';\n  let r = 1.0;\n\n  function updateRValue() {\n    container.querySelector('#rValue').textContent = r.toFixed(2);\n  }\n\n  function computeA(r, type) {\n    if (type === 'sc') return 2 * r;\n    if (type === 'bcc') return (4 * r) \/ Math.sqrt(3);\n    if (type === 'fcc') return (4 * r) \/ Math.sqrt(2);\n  }\n\n  function getFormula(type) {\n    if (type === 'sc') return 'a = 2r';\n    if (type === 'bcc') return 'a = \\\\\\\\frac{4r}{\\\\\\\\sqrt{3}}';\n    if (type === 'fcc') return 'a = \\\\\\\\frac{4r}{\\\\\\\\sqrt{2}}';\n  }\n\n  function addUnitCellLines(a) {\n    const lines = [\n      {start:{x:0,y:0,z:0},end:{x:a,y:0,z:0}},\n      {start:{x:a,y:0,z:0},end:{x:a,y:a,z:0}},\n      {start:{x:a,y:a,z:0},end:{x:0,y:a,z:0}},\n      {start:{x:0,y:a,z:0},end:{x:0,y:0,z:0}},\n      {start:{x:0,y:0,z:a},end:{x:a,y:0,z:a}},\n      {start:{x:a,y:0,z:a},end:{x:a,y:a,z:a}},\n      {start:{x:a,y:a,z:a},end:{x:0,y:a,z:a}},\n      {start:{x:0,y:a,z:a},end:{x:0,y:0,z:a}},\n      {start:{x:0,y:0,z:0},end:{x:0,y:0,z:a}},\n      {start:{x:a,y:0,z:0},end:{x:a,y:0,z:a}},\n      {start:{x:a,y:a,z:0},end:{x:a,y:a,z:a}},\n      {start:{x:0,y:a,z:0},end:{x:0,y:a,z:a}}\n    ];\n    lines.forEach(line => viewer.addLine({...line, color: 'black'}));\n  }\n\n  function addAtoms(a, type) {\n    const positions = [];\n    [0,a].forEach(x => [0,a].forEach(y => [0,a].forEach(z => positions.push({x,y,z}))));\n    if(type==='bcc') positions.push({x:a\/2,y:a\/2,z:a\/2});\n    if(type==='fcc'){\n      positions.push({x:a\/2,y:a\/2,z:0});\n      positions.push({x:a\/2,y:a\/2,z:a});\n      positions.push({x:a\/2,y:0,z:a\/2});\n      positions.push({x:a\/2,y:a,z:a\/2});\n      positions.push({x:0,y:a\/2,z:a\/2});\n      positions.push({x:a,y:a\/2,z:a\/2});\n    }\n    positions.forEach(pos => viewer.addSphere({center:pos, radius:r, color:'green'}));\n  }\n\n  function updateVisualization() {\n    viewer.clear();\n    const a = computeA(r, latticeType);\n    addUnitCellLines(a);\n    addAtoms(a, latticeType);\n    const formulaText = getFormula(latticeType)+` \\\\\\\\approx ${a.toFixed(2)}`+` (for r=${r.toFixed(2)})`;\n    container.querySelector('#formula').innerHTML = `\\\\\\\\(${formulaText}\\\\\\\\)<br>Relationship: Atoms touch along nearest neighbor.`;\n    MathJax.Hub.Queue([\"Typeset\", MathJax.Hub, container.querySelector('#formula')]);\n    viewer.zoomTo();\n    viewer.render();\n  }\n\n  window.setLattice = function(type){\n    latticeType = type;\n    updateVisualization();\n  };\n\n  container.querySelector('#radiusSlider').addEventListener('input', e=>{\n    r = parseFloat(e.target.value);\n    updateRValue();\n    updateVisualization();\n  });\n\n  updateRValue();\n  updateVisualization();\n});\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Atomic Radius (r): 1.00 Simple Cubic Body-Centered Cubic Face-Centered Cubic<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-284","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/learn-by-animation.com\/index.php?rest_route=\/wp\/v2\/pages\/284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn-by-animation.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learn-by-animation.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learn-by-animation.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-by-animation.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=284"}],"version-history":[{"count":3,"href":"https:\/\/learn-by-animation.com\/index.php?rest_route=\/wp\/v2\/pages\/284\/revisions"}],"predecessor-version":[{"id":289,"href":"https:\/\/learn-by-animation.com\/index.php?rest_route=\/wp\/v2\/pages\/284\/revisions\/289"}],"wp:attachment":[{"href":"https:\/\/learn-by-animation.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}