SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Refractor Problem

    Hi,

    I would like to do the refractoring for the re-use. However, I have some problems in doing it with the following coding:

    Code:
        var prismVertexPositionBuffer;
        var prismVertexPositionBuffer2;
        var prismVertexPositionBuffer3;
        var prismVertexNormalBuffer;
        var prismVertexColorBuffer;
        var prismVertexColorBuffer2;
        var prismVertexIndexBuffer;
        var prismVertexIndexBuffer2;
        var prismVertexIndices;
        var prismVertexIndices2;
        var unpackedColors;
        var vectices;
        var normalData;
        var height = 2;
        var n = 12
        var twoPi = 2.0 * 3.14159;
    
        function initBuffers() {
            vertices = [0, 0, 0];
            vertices2 = [0, height, 0];
            vertices3 = [];
            normalData = [0, 0, 1];
            unpackedColors = [1, 0, 0, 1];
            unpackedColors2 = [];
            prismVertexIndices = [];
            prismVertexIndices2 = [];
    
            for (var j = 0; j < n; j++) {
                vertices = vertices.concat([Math.cos((j - 1) * twoPi / n), 0.0, Math.sin((j - 1) * twoPi / n)]);
                vertices2 = vertices2.concat([Math.cos((j - 1) * twoPi / n), height, Math.sin((j - 1) * twoPi / n)]);
                vertices3 = vertices3.concat([Math.cos((j - 1) * twoPi / n), 0.0, Math.sin((j - 1) * twoPi / n)]);
                vertices3 = vertices3.concat([Math.cos((j - 1) * twoPi / n), height, Math.sin((j - 1) * twoPi / n)]);
                normalData = normalData.concat([0, 0, 1]);
                unpackedColors = unpackedColors.concat([1.0, 0.0, 0.0, 1.0]);
                unpackedColors2 = unpackedColors2.concat([1.0, 0.5, 0.0, 1.0]);
                unpackedColors2 = unpackedColors2.concat([1.0, 0.5, 0.0, 1.0]);
                prismVertexIndices = prismVertexIndices.concat([0, j + 1, (j + 1 < n ? j + 2 : 1)]);
                prismVertexIndices2 = prismVertexIndices2.concat([j * 2, j * 2 + 1, (j + 1 < n ? j * 2 + 2 : 1)]);
                prismVertexIndices2 = prismVertexIndices2.concat([j * 2 + 1, (j + 1 < n ? j * 2 + 2 : 1), (j + 1 < n ? j * 2 + 3 : 2)]);
            }
    }
    I want to extract the content of initBuffer() into another js file as a function. However, when I do it, the parameters cannot be returned. Is there any methods to refractoring this code?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Is the above code working code, or is it a part of your attempt to refactor something?

    The function relies on external values, those being height, n, and twoPi

    If you want the function to return a result set, instead of modifying global variables, that can be done too.

    Basically, more information is needed concerning just what you're wanting to achieve.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •