Mastering angle calculations in three.js

My current task involves creating a series of arcs using 102 lines (start point, endpoint, and 100 curve points)

However, I'm facing an issue when the start point of an arc has a higher value than the end point. For instance:

Start Point: 359
End Point: 88

In this scenario, the arc should fall within the empty area of the circle. See the example here: http://jsfiddle.net/XsjgH/1/

I have attempted various solutions, such as:

            function getARC(x, y, r, a){
    a = a * (Math.PI/180);
    var ax = +x + +r * Math.cos(+a),
        ay = +y + +r * Math.sin(+a),
        res = [];
        res['x'] = ax,
        res['y'] = ay;

    return res; 
}
function DRAWarc(cx, cy, ra, sa, ea){
        var arcFactor = 1;
            if(+sa > +ea){
                arcFactor = -1;
                var material = new THREE.LineBasicMaterial({
                    color: 0xff00f0,
                });
            }else{
                var material = new THREE.LineBasicMaterial({
                    color: 0x0000ff,
                });
            }


            var geometry = new THREE.Geometry();

                var s = getARC(cx, cy, ra, sa);
                geometry.vertices.push(new THREE.Vector3(s['x'], s['y'], 0));

                var step = (+ea - +sa)/100;
                var pass = 0;
                var reset = 0;
                for(var i=1;i<=100;i++){
                    if(+sa > +ea && ((+sa + (+step * +i))*arcFactor) < 360 && reset == 0){
                        pass = 1;
                    }
                    if(((+sa + (+step * +i))*arcFactor) < sa){
                        reset = 1;
                        pass = 0;
                    }
                    if(((+sa + (+step * +i))*arcFactor) < ea || pass == 1){
                        var t = getARC(cx, cy, ra, ((+sa + (+step * +i))*arcFactor));
                        geometry.vertices.push(new THREE.Vector3(t['x'], t['y'], 0));
                        //alert((+sa + (+step * +i)));
                    }
                }
                reset = 0;
                var f = getARC(cx, cy, ra, ea);
                geometry.vertices.push(new THREE.Vector3(f['x'], f['y'], 0));

            var line = new THREE.Line(geometry, material);
            scene.add(line);

        }

Answer №1

After some investigation, I believe I have managed to solve the issue at hand.

I inserted the following code to determine the "step" between points on the arc in cases where there may be problematic arcs. In essence, this code resets the start angle to zero and then calculates the step accordingly.

It seems the root of the problem stemmed from having a negative step value.

var step;
if(sa > ea){
    var offset = 360 - sa;
    step = (+ea + offset)/100;
} else {
    step = (+ea - +sa)/100;
}

For reference, here is the link to the fiddle showcasing this code snippet: http://jsfiddle.net/475Ga/

Update: The issue with (sa > ea) stemmed from comparing the length of the string, so be sure to add a "+" before each variable to rectify this.

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

Merge jQuery with the jetpack infinite scroll feature

Utilizing the jQuery code below for an accordion effect on all my WordPress posts on the front end. (function($) { function initAccordion() { var $ele = $('.entry-content').hide(); $(".entry-header").unbind('click&apos ...

Do not fulfill the promise until all the images have finished loading

Below is the intended process: Iterate through a collection of img tags Retrieve each tag's src URL Convert it to a base64 encoded string using an HTML 5 canvas Once all images have been converted, resolve the promise and call the callback function ...

transferring the value of a textbox into another textbox

I am trying to extract the value from one textbox and transfer it to another textbox. Here is my current code: <input type="text" value="Keyword" id="one" /> <input type="text" value="Search" id="two" /> <a href="#" id="btn">button</ ...

In JavaScript, merging objects will exclusively result in an identifier being returned

When working with mongoose, I have encountered an issue where combining data from multiple finds only displays the id instead of the entire object. Interestingly, when I use console.log() on the object directly, it shows all the contents. Below are snippe ...

Retrieve a single document using Angularfire2 without setting up a listener

I'm facing an issue with angularfire2 v6 and angular 11. Specifically, I am attempting to retrieve a single document from the users collection based on their email without utilizing valueChanges() or snapshotChanges(). Realtime updates are not necessa ...

Having trouble linking a sqlite file in your tauri + vue project?

After successfully installing tauri-plugin-sql by adding the specified content to src-tauri/Cargo.toml : [dependencies.tauri-plugin-sql] git = "https://github.com/tauri-apps/plugins-workspace" branch = "v1" features = ["sqlite" ...

What is the best way to customize a MaterialUI outlined input using a global theme overrides file?

I've been working on customizing my theme file with overrides, and I've encountered a strange bug while trying to style the outlined input. It seems like there are two borders appearing when these styles are implemented. https://i.stack.imgur.co ...

I'm curious about how I can selectively utilize vuex-persistedstate with Nuxt for certain stores only

Check out this library: https://github.com/robinvdvleuten/vuex-persistedstate. I have customized the provided plugin file for Nuxt. import createPersistedState from 'vuex-persistedstate' export default ({ store }) => { createPersistedState ...

React - warning - Avoid using <Link> outside of a <Router> context

Warning: Invariant failed: It is not recommended to use <Link> outside of a <Router> Encountering this while attempting to write a test for a component where test("renders post list div ", () => { const posts = [ { created: ...

I'm stuck trying to figure out all the parameters for the MapsPage component in Angular 2

Currently, I am utilizing Angular2 with Ionic2 for my mobile app development. Everything was working flawlessly until I decided to incorporate a new module for Google Maps navigation. Specifically, I am using phonegap-launch-navigator for this purpose. The ...

Switch the Div's orientation from left to right

I'm attempting to toggle a div from left to right, but the code I currently have only toggles it from top to bottom. How can I modify it to slide from left to right instead? jQuery: $(document).ready(function(e) { $('#button').on(' ...

Passing props from Vue3 to a component being rendered through RouterView

I'm facing an issue with the following code snippet: <template> <router-view /> </template> <script setup lang="ts"> ... const product: Ref<IProduct|undefined>: ref(); ... </script> The challenge is ...

jQuery on-click event malfunctioning as expected

I'm currently developing a project that utilizes the GIPHY API to retrieve GIFs. Each time a search is performed, I am storing the search history as individual buttons which users can click on to view the results without needing to re-enter the search ...

Sending the ID of a mapped button to a component

I need help with my React mapping function that displays a series of cards, each with its own button to open up a dialog box. Right now, I'm struggling to pass the unique ID from each object to the correct dialog box. Instead, all IDs are being passed ...

Exploring the Strategy of Incorporating Dynamic Keys into TypeScript for Easy Referencing

I am facing a scenario where I receive keys from the backend and need to design an interface based on these keys. By creating a dynamic interface, I can easily bind these properties. const KEYS_FROM_API = ['ARE_YOU_SURE', 'NOT_NOW', &ap ...

I am attempting to load the ajax content into separate divs simultaneously

When I attempt to load the ajax into two separate divs, I notice that despite calling for data to be placed in two different divs within the ajax code, it all ends up in one div. <script>$(document).ready(function(){ $.ajax({ url: "http: ...

Generating distinctive content within the confines of the Selenium WebDriver

Is there a way to generate a unique username value for the signup page username textbox using selenium webdriver instead of hardcoding it? For example: driver.findElement(By.id("username")).sendKeys("Pinklin") ; When "Pinklin" is hardcoded, running the ...

In TypeScript, how are angle brackets like methodName<string>() utilized?

Could someone please assist me in understanding why we use angular brackets <> in typescript? For example, I have provided some code below and would appreciate an explanation. export class HomePage { constructor(public navCtrl: NavController) ...

Is it possible to duplicate this jQuery/Javascript feature using PHP?

I have the code to fetch tweets in JavaScript, but I need it converted to PHP. Can anyone provide any guidance on how to achieve this? $(document).ready( function() { var url = "http://twitter.com/status/user_timeline/joebloggs.json?count=1 ...

Create a series of vertical lines using jQuery

I'm dealing with a JSON data set that I want to use to generate a grid. In addition to the grid, I also need to display vertical lines above it based on certain values: var arr = []; arr= [ {"Measure":"Air Pollution","Rank":"1","Value":"15.5"}, ...