1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.InputAdapter; 21 import com.badlogic.gdx.graphics.Color; 22 import com.badlogic.gdx.graphics.GL20; 23 import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 24 import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 25 import com.badlogic.gdx.math.DelaunayTriangulator; 26 import com.badlogic.gdx.math.MathUtils; 27 import com.badlogic.gdx.tests.utils.GdxTest; 28 import com.badlogic.gdx.utils.FloatArray; 29 import com.badlogic.gdx.utils.ShortArray; 30 31 /** @author Nathan Sweet */ 32 public class DelaunayTriangulatorTest extends GdxTest { 33 private ShapeRenderer renderer; 34 FloatArray points = new FloatArray(); 35 ShortArray triangles; 36 DelaunayTriangulator trianglulator = new DelaunayTriangulator(); 37 long seed = MathUtils.random.nextLong(); 38 create()39 public void create () { 40 renderer = new ShapeRenderer(); 41 42 triangulate(); 43 System.out.println(seed); 44 45 Gdx.input.setInputProcessor(new InputAdapter() { 46 public boolean touchDown (int screenX, int screenY, int pointer, int button) { 47 seed = MathUtils.random.nextLong(); 48 System.out.println(seed); 49 triangulate(); 50 return true; 51 } 52 53 public boolean mouseMoved (int screenX, int screenY) { 54 triangulate(); 55 return false; 56 } 57 }); 58 } 59 triangulate()60 void triangulate () { 61 // seed = 4139368480425561099l; 62 // seed = 6559652580366669361l; 63 MathUtils.random.setSeed(seed); 64 65 int pointCount = 100; 66 points.clear(); 67 for (int i = 0; i < pointCount; i++) { 68 float value; 69 do { 70 value = MathUtils.random(10, 400); 71 } while (points.contains(value)); 72 points.add(value); 73 do { 74 value = MathUtils.random(10, 400); 75 } while (points.contains(value)); 76 points.add(value); 77 } 78 points.add(Gdx.input.getX()); 79 points.add(Gdx.graphics.getHeight() - Gdx.input.getY()); 80 81 triangles = trianglulator.computeTriangles(points, false); 82 } 83 render()84 public void render () { 85 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 86 87 renderer.setColor(Color.RED); 88 renderer.begin(ShapeType.Filled); 89 for (int i = 0; i < points.size; i += 2) 90 renderer.circle(points.get(i), points.get(i + 1), 4, 12); 91 renderer.end(); 92 93 renderer.setColor(Color.WHITE); 94 renderer.begin(ShapeType.Line); 95 for (int i = 0; i < triangles.size; i += 3) { 96 int p1 = triangles.get(i) * 2; 97 int p2 = triangles.get(i + 1) * 2; 98 int p3 = triangles.get(i + 2) * 2; 99 renderer.triangle( // 100 points.get(p1), points.get(p1 + 1), // 101 points.get(p2), points.get(p2 + 1), // 102 points.get(p3), points.get(p3 + 1)); 103 } 104 renderer.end(); 105 } 106 resize(int width, int height)107 public void resize (int width, int height) { 108 renderer.getProjectionMatrix().setToOrtho2D(0, 0, width, height); 109 renderer.updateMatrices(); 110 } 111 } 112