Creating a spherical shape without relying on SphereGeometry: Is it possible?

I am attempting to create a sphere without utilizing SphereGeometry. My approach involves drawing the sphere using latitudes and longitudes. Below is the code snippet I am working with:

for (var phi = -Math.PI / 2; phi < Math.PI / 2; phi += Math.PI / 15) {
                    var longVertices = new THREE.Geometry()
                    for (var theta = 0; theta <= 2 * Math.PI; theta += Math.PI/2 ) {

                        longitudes = this.point[this.numberOfVertices] = new THREE.Vector3();

                        longitudes.x = origin.x + this.radius * Math.cos(theta) * Math.cos(phi);
                        longitudes.y = origin.z + Math.sin(theta) * this.radius;
                        longitudes.z = origin.y + this.radius * Math.cos(theta) * Math.sin(phi);

                        this.numberOfVertices++;
                        longVertices.vertices.push(longitudes);
                    }
                    longVertices.vertices.push(longVertices.vertices[0]);
                    longVerticesArr.push(longVertices);
                }

This piece of code enables me to draw the longitudes. https://i.sstatic.net/6k4aC.gif

And:

        for (var phi = -Math.PI / 2; phi < Math.PI / 2; phi += Math.PI / 15) {

            var delta = Math.cos(phi) * this.radius;
            var fixedY = Math.sin(phi) * this.radius * direction;
            var latVertices = new THREE.Geometry();
            for (var theta = 0; theta < 2 * Math.PI; theta += Math.PI / 10) {
                latitudes =/* this.point[this.numberOfVertices] =*/ new THREE.Vector3();

                latitudes.z = origin.z + delta * Math.sin(theta);
                latitudes.y = fixedY;
                latitudes.x = origin.x + delta * Math.cos(theta);

                this.numberOfVertices++;

                latVertices.vertices.push(latitudes);
               }
            latVertices.vertices.push(latVertices.vertices[0]);
            latVerticesArr.push(latVertices);

        }

This code snippet assists me in drawing latitudes.

https://i.sstatic.net/ERILj.gif

Currently, I am encountering an issue where I am unable to obtain the points where both latitudes and longitudes intersect. How can I accurately determine these intersection points?

Answer №1

Here is a simple example of a nested loop in action: [ http://jsfiddle.net/cdjtdkwa/ ]

var R = 18; // setting radius value
var LON = 32; var LAT = 16; // approximations
var PILAT = Math.PI/LAT;
var PILON = 2*Math.PI/LON;    
var cos1,cos2,sin1,sin2,t1,t2;    
var y1,y2,r1,r2,t1,t2;    
var geometry = new THREE.Geometry();

for (var i=0; i<LAT; i++) { // iterating through latitude segments
    t1 = Math.PI - i*PILAT;
    t2 = Math.PI - (i+1)*PILAT;

    y1 = Math.cos(t1); // calculate y-position for 1st latitude
    y2 = Math.cos(t2); // calculate y-position for 2nd latitude

    r1 = Math.abs( Math.sin(t1) ); // calculate radius for 1st latitude
    r2 = Math.abs( Math.sin(t2) ); // calculate radius for 2nd latitude         

    for (var j=0; j<LON; j++) { // iterating through longitude segments
        t1 = j*PILON;
        t2 = (j+1)*PILON;

        cos1 = Math.cos(t1);
        cos2 = Math.cos(t2);
        sin1 = Math.sin(t1);
        sin2 = Math.sin(t2);

        geometry.vertices.push(
            new THREE.Vector3( r1*cos1, y1, r1*sin1 ),
            new THREE.Vector3( r2*cos1, y2, r2*sin1 ),
            new THREE.Vector3( r2*cos2, y2, r2*sin2 )          
        );

    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looping through a PHP foreach function, assigning a distinct identifier for the select element using getElementById

My goal is to extract the price value, $option_value['price'], from a dropdown menu as a string rather than using .value, which fetches something different. I am working with select menus generated in a foreach() loop. Each dropdown menu contain ...

Passing a Typescript object as a parameter in a function call

modifications: { babelSetup?: TransformationModifier<babel.Configuration>, } = {} While examining some code in a React project, I came across the above snippet that is passed as an argument to a function. As far as I can tell, the modifications p ...

The never-ending scrolling problem: Autoscroll refusing to halt - a perplexing conundrum in the world

I'm having an issue with my chat box continuously autoscrolling and not allowing me to scroll up. I know where the problem is, but I'm unsure of how to resolve it. Ideally, I want the chat box to autoscroll while still maintaining the ability to ...

Algorithm for detecting collisions in Javascript

I am looking to implement a collision detection function in JavaScript for canvas. Specifically, I have coin and piggy bank objects and want to create a function that triggers the disappearance of a coin object when it comes into contact with a piggy bank. ...

Webpack fails to handle CSS background images

I'm having trouble with my Webpack configuration as it's not processing CSS images set in the background property: background: url('./hero.jpg') no-repeat right; This is resulting in an error message that reads: ERROR in ./src/app/comp ...

Cannot seem to get my AJAX code working correctly to show comments

Having some trouble with my comment system code. The PHP part is working fine, comments are being inserted into the table without any issues. However, I am struggling with the Ajax part. The comments are not being displayed unless the page is reloaded. C ...

Access environmental variables within Next.js middleware

Within my nextjs project, I have declared variables in both the .env and next.conf.js files. The code snippet from the next.conf.js file looks like this: module.exports = { env: { NEXT_PUBLIC_JWT_SECRET: "...", }, publicRuntimeConfig: { ...

What is causing my Vue leave transition to malfunction?

Currently, I am utilizing vee validate for my form validation. Specifically, I have implemented vue transition to handle the display of validation errors in the following manner: <input type="text" name="name" v-validate="'required'"> < ...

Utilizing a single v-model for several elements

I am having an issue with a dropdown that is set to v-model="compose.Recipient". Based on the value of "compose.Recipient", I need another dropdown to appear as shown below: <div class="form-group" v-if="compose.Recipient==2" title="Select Class"> ...

Toggle button in React following a list iteration

Upon receiving data from an API call to Google Books, I want to hide the description paragraphs and implement a toggle button using the "hidden" CSS class from Tailwind CSS. Currently, I am just logging the elements on the "view description" button and uns ...

Tips for displaying specific information using Javascript depending on the input value of an HTML form

I am working on an HTML form that includes a dropdown list with four different names to choose from. window.onload = function(){ document.getElementById("submit").onclick = displaySelectedStudent; } function displaySelectedStu ...

JavaScript Promise Fundamentals

While I am quite familiar with coding in JavaScript, the benefits of promises in the JS world still seem somewhat unclear to me. Below is an example of asynchronous calls using callbacks nested within each other. (function doWorkOldSchool() { setTime ...

Change the function to utilize an HTML class rather than an ID

I am currently working on a resize image function that requires the ID of a file input element as an input. The function takes the image in the form and outputs a resized canvas of the image. However, I recently made changes to the form structure from a st ...

Help! I have a dilemma with my duck: the gltf object loaded from the three.js loader does not seem

I'm currently working on simplifying the threejs glTF loader example by only loading the duck model. If you take a look at what I have so far, you'll notice that when I click and drag, the OrbitControls allow the cube to rotate accordingly but th ...

Express Producing Empty Axios Post Request Body

I am facing an issue with sending two text data pieces from my React frontend to an Express backend. Whenever I use the post command with Axios, the body appears as {} in the backend and becomes unusable. Below is the code that I am using. Client (App.js) ...

What are the steps to retrieve marker data from a MySQL database and showcase them dynamically on a Google Map using AJAX? Addressing time constraints

Utilizing a combination of JavaScript, Google Maps API, PHP, and MySQL, my app is designed to extract a list of markers from a MySQL database, export them to a myXML.xml file, and then load these markers onto the map each time it loads. <!DOCTYPE html& ...

If the user confirms it with a bootstrap dialog box, they will be redirected to the corresponding links

Firstly, I am not interested in using javascript confirm for this purpose. Please read on. For example, adding an onClick attribute to each link with a function that triggers a system dialog box is not what I want. Instead of the standard system dialog bo ...

The element fails to show up when the button is clicked after receiving data from the API

My main goal is to retrieve data from a Weather API upon button click. Once the data is fetched, I want it to be displayed inside a component called Weather on the screen. Currently, I am successfully fetching the data but the Weather component is not disp ...

The timer countdown is encountering an issue where it cannot update the 'textContent' property of a null element

I'm encountering errors with a countdown script that I can't seem to resolve. The error message is generating 1 error every second: Uncaught TypeError: Cannot set property 'textContent' of null at (index):181 (anonymous) @ (index):181 ...

Obtain the user's email using nodemailer

I created a contact form using Nodemailer that I am having trouble with. Take a look at the code below: let mailOptions = { from: '<<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e3636360e363636602d2123">[emai ...