Comment créer un objet ponctuel contenant x, y et créer son tableau? afin que je puisse boucler sur ces points, ajouter/supprimer des points de manière dynamique.Comment créer un tableau de points?
7
A
Répondre
17
var points = [{x:45, y:64}, {x:56, y:98}, {x:23, y:44}];
var len = points.length;
for(var i = 0; i < len; i++) {
alert(points[i].x + ' ' + points[i].y);
}
// to add more points, push an object to the array:
points.push({x:56, y:87});
2
Je vous suggère de lire à propos JavaScript arrays pour apprendre tout cela. Il est important que vous connaissiez les bases.
Exemple pour ajouter:
var points = [];
points.push({x:5, y:3});
7
Vous pouvez créer un constructeur pour un objet Point comme celui-ci:
function Point(x, y) {
this.x = x;
this.y = y;
}
Maintenant, vous pouvez créer des objets Point en utilisant le mot-clé new
:
var p = new Point(4.5, 19.0);
Pour créer un tableau d'objets Point, créez simplement un tableau et placez Point objets: il
var a = [ new Point(1,2), new Point(5,6), new Point(-1,14) ];
Ou:
var a = [];
a.push(new Point(1,2));
a.push(new Point(5,6));
a.push(new Point(-1,14));
Vous utilisez l'opérateur .
pour accéder aux propriétés de l'objet Point. Exemple:
alert(a[2].x);
Ou:
var p = a[2];
alert(p.x + ',' + p.y);
1
plus rapide, plus efficace:
var points = [ [45,64], [56,98], [23,44] ];
for(var i=0, len=points.length; i<len; i++){
//put your code here
console.log('x'+points[i][0], 'y'+points[i][1])
}
// to add more points, push an array to the array:
points.push([100,100]);
L'efficacité ne sera réellement perceptible dans un tableau très grand nombre de points.
Questions connexes
- 1. Java, en utilisant un tableau de points
- 2. Comment trier un tableau d'objets (Points) en Java?
- 3. Comment créer un tableau d'images?
- 4. Comment créer un tableau de pointeurs?
- 5. Comment créer un tableau de boutons Objet?
- 6. Comment créer un tableau de boutons C#?
- 7. Comment créer un tableau d'un tableau de structures
- 8. Comment créer un tableau dans un autre tableau?
- 9. Comment remplir un tableau bidimensionnel avec des points flottants?
- 10. Créer un tableau de fractions
- 11. Créer un tableau de grille
- 12. Comment créer un tableau bidimensionnel en Perl?
- 13. Comment créer un tableau 2D dans Matlab?
- 14. Comment créer un tableau d'entiers en Python?
- 15. Comment créer un nouveau tableau AnyType []?
- 16. Comment créer dynamiquement un tableau associatif
- 17. comment créer chaîne de tableau
- 18. Comment créer un tableau binaire dans VbScript?
- 19. Ajout d'un tableau de points flottants
- 20. C# Comment créer un tableau d'étiquettes
- 21. Comment créer un nouveau tableau dans VB.NET?
- 22. Comment créer un tableau à partir d'un autre tableau?
- 23. créer un tableau js dynamiquement?
- 24. ligne croisée et tableau de points?
- 25. pour créer un composant de tableau blanc
- 26. Suivi: Créer un tableau d'objets de classname
- 27. Créer un tableau PHP à partir de la colonne MySQL
- 28. Créer un tableau de tableaux avec Linq
- 29. Comment créer un tableau de vecteurs de chaînes en Java?
- 30. MySQL Créer un tableau récapitulatif?