Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
problema con Math.max(ARRAY)-Math.min(Array) en un objeto

Tengo problemas para obtener el ancho/alto de una forma de triángulo en puntos/coordenadas de registro de objetos. Lo uso para generar como SVG o para CanvasRenderer2d. Lo probé con una función drawSVG y el dibujo está bien, es solo el ancho y la altura con los que tengo un error.

Mi código aligerado es así (sin descartar propiedades relevantes como grosor de línea, colores...):

 function Triangle(name, arrayX, arrayY){ return { name:name, x:arrayX, y:arrayY, width:Math.max(this.x)-Math.min(this.x), height:Math.max(this.y)-Math.min(this.y), getWidth:function(){return Math.max(this.x)-Math.min(this.x) }, getHeight:function(){return Math.max(this.x)-Math.min(this.x) } } } let tri=Triangle('tri', [15, 42, 84], [10, 37, 48]) console.log(tri.width)//-- NaN console.log(tri.getWidth())//-- NaN /** i've tried using ... this way and having same issue: width:Math.max(...this.x)-Math.min(...this.x), height:Math.max(...this.y)-Math.min(...this.y), getWidth:function(){return Math.max(...this.x)-Math.min(...this.x) }, getHeight:function(){return Math.max(...this.x)-Math.min(...this.x) } let tri=ShapeTriangle('tri', [15, 42, 84], [10, 37, 48]) console.log(tri.width)//-- NaN console.log('tri.getWidth => '+tri.getWidth())//-- NaN */ /**----------------------------------------------- * more complete "real" code use extended object (inheritance) like this * but i don't think it's where is the trouble is function PolygonShape(arrayX,arrayY){//--Abstract return {primitive:'PolygonShape', type:'PolygonShapeAbstract', name:null, x:arrayX,y:arrayY, component:new Map(), list:[] //-- others methods such move coordinates and hit tests } } function ShapeTriangle(name, x, y, x1, y1,x2, y2){ let tri=PolygonShape([x,x1,x2],[y,y1,y2]) tri.type='PolygonShapeTriangle' tri.name=name tri.width=Math.max(this.x)-Math.min(this.x) tri.getWidth=function(){return Math.max(this.x)-Math.min(this.x)} tri.heigth= Math.max(this.y)-Math.min(this.y) tri.getHeight=function(){return Math.max(this.y)-Math.min(this.y)} return tri }*/

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Tienes 2 problemas con tu código.

  1. Está llamando a this.x que no está definido, intente usar arrayX y arrayY.

  2. Debe usar el operador de propagación, las funciones Math.max y Math.min requieren múltiples argumentos, no un solo argumento.

Prueba lo siguiente:

 function Triangle(name, arrayX, arrayY){ return { name:name, x:arrayX, y:arrayY, width:Math.max(...arrayX)-Math.min(...arrayX), height:Math.max(...arrayY)-Math.min(...arrayY), getWidth:function(){return Math.max(...arrayX)-Math.min(...arrayX) }, getHeight:function(){return Math.max(...arrayX)-Math.min(...arrayX) } } } let tri=Triangle('tri', [15, 42, 84], [10, 37, 48]) console.log(tri.width)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda