如何根据高度对JavaScript中的矩形进行排序

我正在寻找一个程序,当按下空格键时,该程序将升序排列不同大小的矩形。我尝试排序,但即使排序后也能得到相同的结果。


到目前为止,这是我尝试过的。我正在使用Brackets在JS中进行编码。下面的代码仅用于排序,这是我的主要问题。一次,我得到了解决方案,我可以用任何键对其进行排序。


请帮帮我。


function C_ShapeRect(l_x,l_y,l_w,l_h,l_c)

{

    this.Xpos = l_x ;

    this.Ypos = l_y ;

    this.Width = l_w ;

    this.Height = l_h ;

    this.Color = l_c ;


    this.Draw = function m_drawRect(l_canvas,l_context)

    {

        l_context.fillStyle = this.Color ;

        l_context.fillRect(this.Xpos , this.Ypos , this.Width , this.Height);

    };

}


var g_RectArray  = new Array(5);

var g_RectArrayLength = g_RectArray.length ;

var g_RectWidth = 20 ;

var g_RectHeight = [35,20,77,10,50] ;

var g_RectColor = ["red","green","blue","yellow","black"] ;


function f_InitRectObject()

{

    var l_Xpos = 90;

    var l_Ypos = 175;


    for(var i=0 ; i<g_RectArrayLength ; i++)

    {

        g_RectArray[i] = new C_ShapeRect(l_Xpos,l_Ypos,g_RectWidth,-g_RectHeight[i],g_RectColor[i]);

        l_Xpos += g_RectWidth ;

    }

}

f_InitRectObject() ;


function f_DrawRectObject()

{

    for(var i=0 ; i<g_RectArrayLength ; i++)

    {

        g_RectArray[i].Draw(g_canvas,g_context);

    }

}


function f_clearCanvas()

{

    g_context.clearRect(0,0,g_canvas.width,g_canvas.height);

    g_context.strokeRect(0,0,g_canvas.width,g_canvas.height);


var g_tempHeight = [] ;


function f_sortRect(l_array)

{

    for(var i=0 ; i<g_RectArrayLength ; i++)

    {

        for(var j = 0 ; j < (g_RectArrayLength - 1) - i ; j++)

        {

            if(l_array[j] > l_array[j + 1])

            {

                g_tempHeight = l_array[j];

                l_array[j] = l_array[j+1];

                l_array[j+1] = g_tempHeight;

            }

        }

    }

}


function f_GameLoop()

{

    f_sortRect(g_RectArray) ;

    f_clearCanvas() ;

    f_DrawRectObject() ;

}

setInterval(f_GameLoop(),g_timeInterval) ;


素胚勾勒不出你
浏览 229回答 2
2回答

catspeake

&nbsp;<html>&nbsp; &nbsp; <canvas width="300" height="300" id="myCanvas"> </canvas>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; var g_canvas = document.getElementById("myCanvas");&nbsp; &nbsp; &nbsp; &nbsp; var g_context = g_canvas.getContext("2d");&nbsp; &nbsp; &nbsp; &nbsp; var g_FPS = 30;&nbsp; &nbsp; &nbsp; &nbsp; var g_timeInterval = 1000/g_FPS ;&nbsp; &nbsp; &nbsp; &nbsp; var g_rectArray = new Array(10);&nbsp; &nbsp; &nbsp; &nbsp; var g_arrayLength =g_rectArray.length ;&nbsp; &nbsp; &nbsp; &nbsp; var g_rectWidth = 25;&nbsp; &nbsp; &nbsp; &nbsp; var g_rectHeight = [21,2,23,77,45,54,8,88,7,32];&nbsp; &nbsp; &nbsp; &nbsp; var g_rectColor = ["red" , "yellow" , "black" , "blue" , "cyan" , "pink" , "green" , "orange" , "brown" , "magenta"];&nbsp; &nbsp; &nbsp; &nbsp; var g_tempRectObj ;//&nbsp; &nbsp; &nbsp; &nbsp; var g_tempRectXpos ;&nbsp; &nbsp; &nbsp; &nbsp; var g_rectObjSortingIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; var g_startSorting =0;&nbsp; &nbsp; &nbsp; &nbsp; function C_Rectangles(l_x,l_y,l_w,l_h,l_c)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Xpos = l_x ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Ypos = l_y ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Width =l_w ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Height =l_h ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Color = l_c ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Speed&nbsp; =1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.directingSpeed&nbsp; = this.Speed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.targetRectXpos =0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.targetCheck = 1 ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Draw = function m_drawRect(l_ctx)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l_ctx.fillStyle = this.Color ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l_ctx.fillRect(this.Xpos , this.Ypos , this.Width , -this.Height);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Move = function m_moveRect()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(this.targetCheck == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Xpos += this.Speed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(this.Xpos >= this.targetRectXpos && this.Speed > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Xpos = this.targetRectXpos;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.targetCheck = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(this.Xpos <= this.targetRectXpos && this.Speed < 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Xpos = this.targetRectXpos;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.targetCheck = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.initMove = function f_initMove(l_targetPosition)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.targetRectXpos = l_targetPosition ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(this.Xpos < l_targetPosition)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Speed = this.directingSpeed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(this.Xpos > l_targetPosition)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Speed = -this.directingSpeed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.targetCheck = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function f_initRectObject()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var l_Xpos = 20;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var l_Ypos = 200;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i=0 ; i<g_arrayLength ; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_rectArray[i] = new C_Rectangles(l_Xpos,l_Ypos,g_rectWidth,g_rectHeight[i],g_rectColor[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l_Xpos += g_rectWidth ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(g_rectArray);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function f_drawRectObject()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i=0 ; i<g_arrayLength ; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_rectArray[i].Draw(g_context);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function f_moveRectObject()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i=0 ; i<g_arrayLength ; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_rectArray[i].Move();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function f_rectObjectMovementCheck()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i=0 ; i<g_arrayLength ; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(g_rectArray[i].targetCheck == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false ;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function f_clearCanvas()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_context.clearRect(0,0,g_canvas.width,g_canvas.height);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_context.strokeRect(0,0,g_canvas.width,g_canvas.height);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function f_BubbleSort()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(g_startSorting == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(f_rectObjectMovementCheck() == false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(g_rectArray[g_rectObjSortingIndex].Height > g_rectArray[g_rectObjSortingIndex+1].Height)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_rectArray[g_rectObjSortingIndex].initMove(g_rectArray[g_rectObjSortingIndex+1].Xpos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_rectArray[g_rectObjSortingIndex+1].initMove(g_rectArray[g_rectObjSortingIndex].Xpos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_tempRectObj = g_rectArray[g_rectObjSortingIndex];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_rectArray[g_rectObjSortingIndex] = g_rectArray[g_rectObjSortingIndex+1] ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_rectArray[g_rectObjSortingIndex+1] = g_tempRectObj ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_rectObjSortingIndex ++ ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(g_rectObjSortingIndex >= (g_arrayLength -1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_rectObjSortingIndex =0;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function f_GameLoop()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f_BubbleSort();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f_moveRectObject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f_clearCanvas();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f_drawRectObject();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; f_initRectObject();&nbsp; &nbsp; &nbsp; &nbsp; function f_isKeyPressed(l_eventData)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(l_eventData.keyCode == 32)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_startSorting = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; setInterval(f_GameLoop,g_timeInterval);&nbsp; &nbsp; &nbsp; &nbsp; document.addEventListener("keydown" , f_isKeyPressed);&nbsp; &nbsp; </script></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript