├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── manolovn
│ │ └── sample
│ │ ├── SampleActivity.java
│ │ ├── color
│ │ └── BrewerColorGenerator.java
│ │ ├── exporter
│ │ ├── FileUtils.java
│ │ └── ImageExporter.java
│ │ └── point
│ │ └── PointGeneratorFactory.java
│ └── res
│ ├── layout
│ └── activity_main.xml
│ ├── mipmap-hdpi
│ ├── ic_fullscreen.png
│ ├── ic_launcher.png
│ └── ic_refresh.png
│ ├── mipmap-mdpi
│ ├── ic_fullscreen.png
│ ├── ic_launcher.png
│ └── ic_refresh.png
│ ├── mipmap-xhdpi
│ ├── ic_fullscreen.png
│ ├── ic_launcher.png
│ └── ic_refresh.png
│ ├── mipmap-xxhdpi
│ ├── ic_fullscreen.png
│ ├── ic_launcher.png
│ └── ic_refresh.png
│ ├── mipmap-xxxhdpi
│ ├── ic_fullscreen.png
│ └── ic_refresh.png
│ ├── values-v19
│ └── styles.xml
│ └── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── art
└── 001.png
├── build.gradle
├── config
└── checkstyle
│ └── checkstyle.xml
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── trianglify
├── .gitignore
├── build.gradle
├── gradle.properties
├── proguard-rules.pro
└── src
└── main
├── AndroidManifest.xml
├── java
└── com
│ └── manolovn
│ └── trianglify
│ ├── Default.java
│ ├── TrianglifyDrawable.java
│ ├── TrianglifyView.java
│ ├── domain
│ ├── Edge.java
│ ├── Point.java
│ └── Triangle.java
│ ├── generator
│ ├── color
│ │ ├── ColorGenerator.java
│ │ └── RandomColorGenerator.java
│ └── point
│ │ ├── CircularPointGenerator.java
│ │ ├── PointGenerator.java
│ │ └── RegularPointGenerator.java
│ ├── renderer
│ ├── PointRenderer.java
│ └── TriangleRenderer.java
│ ├── triangulator
│ ├── DelaunayTriangulator.java
│ ├── Triangulation.java
│ └── Triangulator.java
│ └── util
│ ├── ClassUtil.java
│ ├── EdgeDistanceComparator.java
│ ├── MathUtils.java
│ └── Preconditions.java
└── res
└── values
├── attrs.xml
└── strings.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 | # Proguard folder generated by Eclipse
23 | proguard/
24 |
25 | # IntelliJ files
26 | .idea
27 | *.iml
28 |
29 | # Maven output folder
30 | target
31 |
32 | # Misc
33 | .DS_Store
34 | Thumbs.db
35 | *.swp
36 | *.bak
37 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 |
3 | android:
4 | components:
5 | - tools
6 | - build-tools-23.0.2
7 | - android-23
8 | - extra-android-support
9 | - extra-google-m2repository
10 | - extra-android-m2repository
11 |
12 | script:
13 | ./gradlew checkstyle build --stacktrace
14 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Trianglify
2 | [](https://travis-ci.org/manolovn/trianglify)
3 | [](https://maven-badges.herokuapp.com/maven-central/com.manolovn/trianglify)
4 | [](http://android-arsenal.com/details/1/3014)
5 |
6 | Android view inspired by http://qrohlf.com/trianglify/
7 |
8 |
9 |
10 | # Usage
11 |
12 | Add the dependency
13 |
14 | ```groovy
15 | dependencies {
16 | compile 'com.manolovn:trianglify:1.1.0'
17 | }
18 | ```
19 |
20 | Just add the view to your layout:
21 |
22 | ```xml
23 |
27 | ```
28 |
29 | Cell size, variance, bleeds, color generator and point generator can be initialized from layout:
30 |
31 | ```xml
32 |
42 | ```
43 |
44 | or using setters (see sample for more info):
45 |
46 | ```java
47 | trianglifyView.getDrawable().setCellSize(175);
48 | trianglifyView.getDrawable().setVariance(75);
49 | trianglifyView.getDrawable().setColorGenerator(new AnyColorGenerator());
50 | trianglifyView.getDrawable().setPointGenerator(new AnyPointGenerator());
51 | ```
52 |
53 | In the sample, you can find an example of ColorGenerator and an example of how to use differents types of PointGenerator. In the component are included two: RegularPointGenerator (for a grid distribution) and CircularPointGenerator (for a concentric circles distribution).
54 |
55 | # License
56 |
57 | Copyright 2015 Manuel Vera Nieto
58 |
59 | Licensed under the Apache License, Version 2.0 (the "License");
60 | you may not use this file except in compliance with the License.
61 | You may obtain a copy of the License at
62 |
63 | http://www.apache.org/licenses/LICENSE-2.0
64 |
65 | Unless required by applicable law or agreed to in writing, software
66 | distributed under the License is distributed on an "AS IS" BASIS,
67 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
68 | See the License for the specific language governing permissions and
69 | limitations under the License.
70 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | buildToolsVersion '26.0.2'
6 |
7 | defaultConfig {
8 | applicationId "com.manolovn.sample"
9 | minSdkVersion 15
10 | targetSdkVersion 27
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | lintOptions {
21 | abortOnError false
22 | }
23 | }
24 |
25 | dependencies {
26 | compile 'com.android.support:appcompat-v7:27.0.2'
27 | compile 'com.android.support:design:27.0.2'
28 | compile 'com.jakewharton:butterknife:8.8.1'
29 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
30 | compile 'com.manolovn:colorbrewer:1.0.1'
31 | compile project(':trianglify')
32 | }
33 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /home/manolo/Android/Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
12 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/manolovn/sample/SampleActivity.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.sample;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.AdapterView;
8 | import android.widget.ArrayAdapter;
9 | import android.widget.SeekBar;
10 | import android.widget.Spinner;
11 | import android.widget.Toast;
12 |
13 | import com.manolovn.colorbrewer.ColorBrewer;
14 | import com.manolovn.sample.color.BrewerColorGenerator;
15 | import com.manolovn.sample.exporter.ImageExporter;
16 | import com.manolovn.sample.point.PointGeneratorFactory;
17 | import com.manolovn.sample.point.PointGeneratorFactory.Type;
18 | import com.manolovn.trianglify.TrianglifyView;
19 |
20 | import java.io.IOException;
21 | import java.util.ArrayList;
22 | import java.util.List;
23 |
24 | import butterknife.BindView;
25 | import butterknife.ButterKnife;
26 | import butterknife.OnClick;
27 |
28 | public class SampleActivity extends AppCompatActivity {
29 |
30 | @BindView(R.id.trianglify)
31 | TrianglifyView trianglifyView;
32 | @BindView(R.id.cellSizeControl)
33 | SeekBar cellSizeControl;
34 | @BindView(R.id.varianceControl)
35 | SeekBar varianceControl;
36 | @BindView(R.id.colorControl)
37 | Spinner colorControl;
38 | @BindView(R.id.pointsControl)
39 | Spinner pointsControl;
40 | @BindView(R.id.controlsContainer)
41 | ViewGroup controlsContainer;
42 |
43 | private ImageExporter exporter;
44 |
45 | @Override
46 | protected void onCreate(Bundle savedInstanceState) {
47 | super.onCreate(savedInstanceState);
48 | setContentView(R.layout.activity_main);
49 | ButterKnife.bind(this);
50 |
51 | initCellSizeControl();
52 | initVarianceControl();
53 | initColorControl();
54 | initPointsControl();
55 |
56 | trianglifyView.setDrawingCacheEnabled(true);
57 | exporter = new ImageExporter();
58 | }
59 |
60 | @OnClick(R.id.saveToGalleryButton)
61 | void saveToGallery() {
62 | exportViewToImage();
63 | }
64 |
65 | @OnClick(R.id.toggleFullscreen)
66 | void toggleFullScreen() {
67 | if (controlsContainer.getVisibility() == View.GONE) {
68 | controlsContainer.setVisibility(View.VISIBLE);
69 | } else if (controlsContainer.getVisibility() == View.VISIBLE) {
70 | controlsContainer.setVisibility(View.GONE);
71 | }
72 | }
73 |
74 | private void exportViewToImage() {
75 | try {
76 | exporter.exportFromView(this, trianglifyView);
77 | Toast.makeText(this, R.string.image_generated_success, Toast.LENGTH_SHORT).show();
78 | } catch (IOException e) {
79 | Toast.makeText(this, R.string.image_generated_failed, Toast.LENGTH_SHORT).show();
80 | }
81 | }
82 |
83 | private void initCellSizeControl() {
84 | cellSizeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
85 | int progress;
86 |
87 | @Override
88 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
89 | this.progress = progress;
90 | }
91 |
92 | @Override
93 | public void onStartTrackingTouch(SeekBar seekBar) {
94 |
95 | }
96 |
97 | @Override
98 | public void onStopTrackingTouch(SeekBar seekBar) {
99 | if (progress > 0) {
100 | trianglifyView.setDrawingCacheEnabled(false);
101 | trianglifyView.getDrawable().setCellSize(progress * 10);
102 | trianglifyView.setDrawingCacheEnabled(true);
103 | }
104 | }
105 | });
106 | }
107 |
108 | private void initVarianceControl() {
109 | varianceControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
110 | int progress;
111 |
112 | @Override
113 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
114 | this.progress = progress;
115 | }
116 |
117 | @Override
118 | public void onStartTrackingTouch(SeekBar seekBar) {
119 |
120 | }
121 |
122 | @Override
123 | public void onStopTrackingTouch(SeekBar seekBar) {
124 | trianglifyView.setDrawingCacheEnabled(false);
125 | trianglifyView.getDrawable().setVariance(progress + 2);
126 | trianglifyView.setDrawingCacheEnabled(true);
127 | }
128 | });
129 | }
130 |
131 | private void initColorControl() {
132 | final List list = new ArrayList<>(ColorBrewer.values().length);
133 | final ColorBrewer[] colors = ColorBrewer.values();
134 | for (ColorBrewer color : colors) {
135 | list.add(color.name());
136 | }
137 |
138 | ArrayAdapter adapter =
139 | new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
140 | adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
141 | colorControl.setAdapter(adapter);
142 |
143 | colorControl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
144 | @Override
145 | public void onItemSelected(AdapterView> parent, View view, int position, long id) {
146 | trianglifyView.setDrawingCacheEnabled(false);
147 | trianglifyView.getDrawable()
148 | .setColorGenerator(new BrewerColorGenerator(colors[position]));
149 | trianglifyView.setDrawingCacheEnabled(true);
150 | }
151 |
152 | @Override
153 | public void onNothingSelected(AdapterView> parent) {
154 |
155 | }
156 | });
157 | }
158 |
159 | private void initPointsControl() {
160 | final List list = new ArrayList<>(Type.values().length);
161 | for (Type type : Type.values()) {
162 | list.add(type.toString());
163 | }
164 |
165 | ArrayAdapter adapter =
166 | new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
167 | adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
168 | pointsControl.setAdapter(adapter);
169 |
170 | pointsControl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
171 | @Override
172 | public void onItemSelected(AdapterView> parent, View view, int position, long id) {
173 | trianglifyView.setDrawingCacheEnabled(false);
174 | trianglifyView.getDrawable().setPointGenerator(
175 | PointGeneratorFactory.from(list.get(position)));
176 | trianglifyView.setDrawingCacheEnabled(true);
177 | }
178 |
179 | @Override
180 | public void onNothingSelected(AdapterView> parent) {
181 |
182 | }
183 | });
184 | }
185 | }
186 |
--------------------------------------------------------------------------------
/app/src/main/java/com/manolovn/sample/color/BrewerColorGenerator.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.sample.color;
2 |
3 | import com.manolovn.colorbrewer.ColorBrewer;
4 | import com.manolovn.trianglify.generator.color.ColorGenerator;
5 |
6 | /**
7 | * Color brewer color generator
8 | *
9 | * @author manolovn
10 | */
11 | public class BrewerColorGenerator implements ColorGenerator {
12 |
13 | private ColorBrewer palette;
14 | private int index = 0;
15 | private int[] colors;
16 |
17 | public BrewerColorGenerator(ColorBrewer palette) {
18 | this.palette = palette;
19 | }
20 |
21 | public void setCount(int count) {
22 | colors = palette.getColorPalette(count);
23 | index = 0;
24 | }
25 |
26 | @Override
27 | public int nextColor() {
28 | int color = colors[index];
29 | index++;
30 | return color;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/manolovn/sample/exporter/FileUtils.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.sample.exporter;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.net.Uri;
6 | import android.os.Environment;
7 |
8 | import java.io.File;
9 |
10 | /**
11 | * File Utils
12 | *
13 | * @author manolovn
14 | */
15 | public class FileUtils {
16 |
17 | private FileUtils() {
18 |
19 | }
20 |
21 | public static File getOrCreateFolder(String folderName) {
22 | File mediaStorageDir = new File(
23 | Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
24 | folderName);
25 | if (!mediaStorageDir.exists()) {
26 | if (!mediaStorageDir.mkdirs()) {
27 | return null;
28 | }
29 | }
30 | return mediaStorageDir;
31 | }
32 |
33 | public static void scanForFile(Context context, String path) {
34 | Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
35 | File f = new File(path);
36 | Uri contentUri = Uri.fromFile(f);
37 | mediaScanIntent.setData(contentUri);
38 | context.sendBroadcast(mediaScanIntent);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/java/com/manolovn/sample/exporter/ImageExporter.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.sample.exporter;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.view.View;
6 |
7 | import java.io.File;
8 | import java.io.FileOutputStream;
9 | import java.io.IOException;
10 |
11 | /**
12 | * Simple image exporter
13 | */
14 | public class ImageExporter {
15 |
16 | private static final String ALBUM_NAME = "Trianglify";
17 |
18 | public ImageExporter() {
19 | }
20 |
21 | public void exportFromView(final Context context, final View view) throws IOException {
22 | File folder = FileUtils.getOrCreateFolder(ALBUM_NAME);
23 |
24 | Bitmap bitmap = view.getDrawingCache(true);
25 |
26 | File file = null;
27 | int number = 1;
28 | boolean creating = true;
29 | while (creating) {
30 | String imageName = String.format("%05d", number);
31 | file = new File(folder + "/" + imageName + ".png");
32 |
33 | if (file.exists()) {
34 | number++;
35 | } else {
36 | creating = false;
37 | }
38 | }
39 |
40 | FileOutputStream fos = new FileOutputStream(file);
41 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
42 |
43 | fos.flush();
44 | fos.close();
45 |
46 | file.createNewFile();
47 | FileUtils.scanForFile(context, file.getPath());
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/java/com/manolovn/sample/point/PointGeneratorFactory.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.sample.point;
2 |
3 | import com.manolovn.trianglify.generator.point.CircularPointGenerator;
4 | import com.manolovn.trianglify.generator.point.PointGenerator;
5 | import com.manolovn.trianglify.generator.point.RegularPointGenerator;
6 |
7 | /**
8 | * Point generator factory
9 | *
10 | * @author manolovn
11 | */
12 | public class PointGeneratorFactory {
13 |
14 | public enum Type {
15 | REGULAR("regular"),
16 | CIRCULAR("circular");
17 |
18 | Type(String type) {
19 |
20 | }
21 | }
22 |
23 | public static PointGenerator from(String stringType) {
24 | Type type = Type.valueOf(stringType);
25 | switch (type) {
26 | case CIRCULAR:
27 | return new CircularPointGenerator();
28 | case REGULAR:
29 | default:
30 | return new RegularPointGenerator();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
11 |
12 |
23 |
24 |
28 |
29 |
33 |
34 |
38 |
39 |
43 |
44 |
48 |
49 |
53 |
54 |
58 |
59 |
63 |
64 |
69 |
70 |
71 |
72 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_fullscreen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-hdpi/ic_fullscreen.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_refresh.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-hdpi/ic_refresh.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_fullscreen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-mdpi/ic_fullscreen.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_refresh.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-mdpi/ic_refresh.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_fullscreen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-xhdpi/ic_fullscreen.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_refresh.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-xhdpi/ic_refresh.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_fullscreen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-xxhdpi/ic_fullscreen.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_refresh.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-xxhdpi/ic_refresh.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_fullscreen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-xxxhdpi/ic_fullscreen.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_refresh.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/app/src/main/res/mipmap-xxxhdpi/ic_refresh.png
--------------------------------------------------------------------------------
/app/src/main/res/values-v19/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 16dp
3 | 16dp
4 | 5dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Trianglify Sample
3 | CELL SIZE
4 | VARIANCE
5 | COLOR
6 | Export to image
7 | Image generated successfully
8 | EXPORTING FAILED
9 | Save to gallery
10 | POINTS DISTRIBUTION
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/art/001.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/art/001.png
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 |
2 | buildscript {
3 | repositories {
4 | jcenter()
5 | google()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:3.0.1'
9 | }
10 | }
11 |
12 | allprojects {
13 | repositories {
14 | jcenter()
15 | google()
16 | }
17 | }
18 |
19 | def isReleaseBuild() {
20 | return version.contains("SNAPSHOT") == false
21 | }
22 |
--------------------------------------------------------------------------------
/config/checkstyle/checkstyle.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | POM_NAME=Trianglify
2 | POM_ARTIFACT_ID=trianglify
3 | POM_PACKAGING=aar
4 | VERSION_NAME=1.1.0
5 | VERSION_CODE=110
6 | GROUP=com.manolovn
7 |
8 | POM_DESCRIPTION=Android view inspired by http://qrohlf.com/trianglify
9 | POM_URL=https://github.com/manolovn/trianglify
10 | POM_SCM_URL=https://github.com/manolovn/trianglify
11 | POM_SCM_CONNECTION=scm:git@github.com:manolovn/trianglify.git
12 | POM_SCM_DEV_CONNECTION=scm:git@github.com:manolovn/trianglify.git
13 | POM_LICENCE_NAME=The Apache Software License, Version 2.0
14 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
15 | POM_LICENCE_DIST=repo
16 | POM_DEVELOPER_ID=manolovn
17 | POM_DEVELOPER_NAME=Manuel Vera Nieto
18 |
19 | ANDROID_BUILD_TOOLS_VERSION=23.0.2
20 | ANDROID_COMPILE_SDK_VERSION=23
21 | ANDROID_TARGET_SDK_VERSION=23
22 | ANDROID_MIN_SDK=15
23 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/manolovn/trianglify/ca2aae4480104b7ade96ea767af53eaae567e16b/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Feb 13 22:34:57 CET 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':trianglify'
2 |
--------------------------------------------------------------------------------
/trianglify/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/trianglify/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'checkstyle'
3 |
4 | android {
5 | compileSdkVersion Integer.parseInt(project.ANDROID_COMPILE_SDK_VERSION)
6 | buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
7 |
8 | defaultConfig {
9 | versionName project.VERSION_NAME
10 | versionCode Integer.parseInt(project.VERSION_CODE)
11 |
12 | minSdkVersion Integer.parseInt(project.ANDROID_MIN_SDK)
13 | targetSdkVersion Integer.parseInt(project.ANDROID_TARGET_SDK_VERSION)
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | lintOptions {
22 | abortOnError false
23 | }
24 | buildToolsVersion '26.0.2'
25 | }
26 |
27 | dependencies {
28 | compile 'com.android.support:appcompat-v7:27.0.2'
29 | }
30 |
31 | apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
32 |
33 | task checkstyle(type: Checkstyle) {
34 | configFile file('../config/checkstyle/checkstyle.xml')
35 | source 'src/main/java'
36 | include '**/*.java'
37 | exclude '**/gen/**'
38 |
39 | classpath = files()
40 | }
41 |
--------------------------------------------------------------------------------
/trianglify/gradle.properties:
--------------------------------------------------------------------------------
1 | POM_NAME=Trianglify
2 | POM_ARTIFACT_ID=trianglify
3 | POM_PACKAGING=aar
4 |
--------------------------------------------------------------------------------
/trianglify/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /home/manolo/Android/Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/trianglify/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/Default.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify;
2 |
3 | import com.manolovn.trianglify.generator.color.ColorGenerator;
4 | import com.manolovn.trianglify.generator.color.RandomColorGenerator;
5 | import com.manolovn.trianglify.generator.point.PointGenerator;
6 | import com.manolovn.trianglify.generator.point.RegularPointGenerator;
7 |
8 | /**
9 | * Container for default values
10 | *
11 | * @author manolovn
12 | */
13 | class Default {
14 |
15 | static int cellSize = 200;
16 | static int variance = 50;
17 | static int bleedX = cellSize;
18 | static int bleedY = cellSize;
19 | static ColorGenerator colorGenerator = new RandomColorGenerator();
20 | static PointGenerator pointGenerator = new RegularPointGenerator();
21 | }
22 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/TrianglifyDrawable.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.ColorFilter;
5 | import android.graphics.PixelFormat;
6 | import android.graphics.Rect;
7 | import android.graphics.drawable.Drawable;
8 | import android.os.AsyncTask;
9 |
10 | import com.manolovn.trianglify.domain.Point;
11 | import com.manolovn.trianglify.domain.Triangle;
12 | import com.manolovn.trianglify.generator.color.ColorGenerator;
13 | import com.manolovn.trianglify.generator.point.PointGenerator;
14 | import com.manolovn.trianglify.renderer.TriangleRenderer;
15 | import com.manolovn.trianglify.triangulator.DelaunayTriangulator;
16 | import com.manolovn.trianglify.triangulator.Triangulator;
17 |
18 | import java.util.Vector;
19 |
20 | /**
21 | * Trianglify drawable
22 | *
23 | * @author doug
24 | */
25 | public class TrianglifyDrawable extends Drawable {
26 |
27 | private PointGenerator pointGenerator;
28 | private Triangulator triangulator;
29 | private TriangleRenderer triangleRenderer;
30 |
31 | private int width;
32 | private int height;
33 | private int bleedX;
34 | private int bleedY;
35 | private int cellSize;
36 | private int variance;
37 |
38 | private Vector points;
39 | private Vector triangles;
40 |
41 | private final Object lock = new Object();
42 | // Used with triangulateInBackground
43 | Boolean ready = false;
44 |
45 | public TrianglifyDrawable(
46 | int bleedX,
47 | int bleedY,
48 | int cellSize,
49 | int variance,
50 | ColorGenerator colorGenerator,
51 | PointGenerator pointGenerator) {
52 | super();
53 |
54 | this.bleedX = bleedX;
55 | this.bleedY = bleedY;
56 | this.cellSize = cellSize;
57 | this.variance = variance;
58 | this.pointGenerator = pointGenerator;
59 |
60 | triangulator = new DelaunayTriangulator();
61 | triangleRenderer = new TriangleRenderer(colorGenerator);
62 | }
63 |
64 | @Override
65 | protected void onBoundsChange(Rect bounds) {
66 | this.width = bounds.width();
67 | this.height = bounds.height();
68 | triangulateInBackground();
69 | }
70 |
71 | @Override
72 | public void draw(Canvas canvas) {
73 | if (ready && triangles != null) {
74 | triangleRenderer.render(triangles, canvas);
75 | }
76 | }
77 |
78 | @Override
79 | public void setAlpha(int alpha) {
80 |
81 | }
82 |
83 | @Override
84 | public void setColorFilter(ColorFilter colorFilter) {
85 |
86 | }
87 |
88 | @Override
89 | public int getOpacity() {
90 | return PixelFormat.OPAQUE;
91 | }
92 |
93 | public void setVariance(int variance) {
94 | this.variance = variance;
95 | triangulateInBackground();
96 | }
97 |
98 | public void setCellSize(int cellSize) {
99 | this.cellSize = cellSize;
100 | triangulateInBackground();
101 | }
102 |
103 | public void setColorGenerator(ColorGenerator colorGenerator) {
104 | triangleRenderer = new TriangleRenderer(colorGenerator);
105 | triangulateInBackground();
106 | }
107 |
108 | public void setPointGenerator(PointGenerator pointGenerator) {
109 | this.pointGenerator = pointGenerator;
110 | triangulateInBackground();
111 | }
112 |
113 | private void triangulateInBackground() {
114 | TriangulateAsyncTask task = new TriangulateAsyncTask();
115 | task.execute();
116 | }
117 |
118 | private class TriangulateAsyncTask extends AsyncTask {
119 |
120 | @Override
121 | protected Void doInBackground(Void... params) {
122 | synchronized (lock) {
123 | ready = false;
124 | pointGenerator.setBleedX(bleedX);
125 | pointGenerator.setBleedY(bleedY);
126 | points = pointGenerator.generatePoints(width, height, cellSize, variance);
127 | if (points.size() >= 3) {
128 | triangles = triangulator.triangulate(points);
129 | }
130 | ready = true;
131 | }
132 | return null;
133 | }
134 |
135 | @Override
136 | protected void onPostExecute(Void aVoid) {
137 | invalidateSelf();
138 | }
139 | }
140 |
141 | }
142 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/TrianglifyView.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.util.AttributeSet;
6 | import android.view.View;
7 |
8 | import com.manolovn.trianglify.generator.color.ColorGenerator;
9 | import com.manolovn.trianglify.generator.point.PointGenerator;
10 | import com.manolovn.trianglify.util.ClassUtil;
11 |
12 | /**
13 | * Trianglify view
14 | *
15 | * @author manolovn
16 | */
17 | public class TrianglifyView extends View {
18 |
19 | private TrianglifyDrawable drawable;
20 |
21 | public TrianglifyView(Context context) {
22 | super(context);
23 | init(null);
24 | }
25 |
26 | public TrianglifyView(Context context, AttributeSet attrs) {
27 | super(context, attrs);
28 | init(attrs);
29 | }
30 |
31 | public TrianglifyView(Context context, AttributeSet attrs, int defStyleAttr) {
32 | super(context, attrs, defStyleAttr);
33 | init(attrs);
34 | }
35 |
36 | private void init(AttributeSet attrs) {
37 | ColorGenerator colorGenerator = Default.colorGenerator;
38 | PointGenerator pointGenerator = Default.pointGenerator;
39 | int cellSize = Default.cellSize;
40 | int variance = Default.variance;
41 | int bleedX = Default.bleedX;
42 | int bleedY = Default.bleedY;
43 |
44 | if (attrs != null) {
45 | TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs,
46 | R.styleable.TrianglifyView, 0, 0);
47 |
48 | try {
49 | cellSize = a.getInteger(R.styleable.TrianglifyView_cellSize, cellSize);
50 | variance = a.getInteger(R.styleable.TrianglifyView_variance, variance);
51 | bleedX = a.getInteger(R.styleable.TrianglifyView_bleedX, bleedX);
52 | bleedY = a.getInteger(R.styleable.TrianglifyView_bleedY, bleedY);
53 | colorGenerator = getColorGeneratorByName(
54 | a.getString(R.styleable.TrianglifyView_colorGenerator));
55 | pointGenerator = getPointGeneratorByName(
56 | a.getString(R.styleable.TrianglifyView_pointGenerator));
57 | } finally {
58 | a.recycle();
59 | }
60 | }
61 |
62 | drawable = new TrianglifyDrawable(bleedX, bleedY, cellSize, variance, colorGenerator,
63 | pointGenerator);
64 | setBackgroundDrawable(drawable);
65 | }
66 |
67 | @Override
68 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
69 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
70 | drawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
71 | }
72 |
73 | public TrianglifyDrawable getDrawable() {
74 | return drawable;
75 | }
76 |
77 | private ColorGenerator getColorGeneratorByName(String className) {
78 | if (className == null || className.isEmpty()) {
79 | return null;
80 | }
81 | return ClassUtil.getClassByName(className, ColorGenerator.class);
82 | }
83 |
84 | private PointGenerator getPointGeneratorByName(String className) {
85 | if (className == null || className.isEmpty()) {
86 | return Default.pointGenerator;
87 | }
88 | return ClassUtil.getClassByName(className, PointGenerator.class);
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/domain/Edge.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.domain;
2 |
3 | /**
4 | * Edge entity
5 | *
6 | * @author manolovn
7 | */
8 | public class Edge {
9 |
10 | public Point a;
11 | public Point b;
12 |
13 | public Edge(Point a, Point b) {
14 | this.a = a;
15 | this.b = b;
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/domain/Point.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.domain;
2 |
3 | /**
4 | * Point entity
5 | *
6 | * @author manolovn
7 | */
8 | public class Point {
9 |
10 | public int x;
11 | public int y;
12 |
13 | public Point(int x, int y) {
14 | this.x = x;
15 | this.y = y;
16 | }
17 |
18 | public Point sub(Point point) {
19 | return new Point(this.x - point.x, this.y - point.y);
20 | }
21 |
22 | public Point add(Point point) {
23 | return new Point(this.x + point.x, this.y + point.y);
24 | }
25 |
26 | public Point mult(int scalar) {
27 | return new Point(this.x * scalar, this.y * scalar);
28 | }
29 |
30 | public double mag() {
31 | return Math.sqrt(this.x * this.x + this.y * this.y);
32 | }
33 |
34 | public double dot(Point point) {
35 | return this.x * point.x + this.y * point.y;
36 | }
37 |
38 | public double cross(Point point) {
39 | return this.y * point.x - this.x * point.y;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/domain/Triangle.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.domain;
2 |
3 | import com.manolovn.trianglify.util.EdgeDistanceComparator;
4 | import com.manolovn.trianglify.util.MathUtils;
5 |
6 | import java.util.Arrays;
7 |
8 | /**
9 | * Triangle entity
10 | *
11 | * @author manolovn
12 | */
13 | public class Triangle {
14 |
15 | public Point a;
16 | public Point b;
17 | public Point c;
18 |
19 | public Triangle(Point a, Point b, Point c) {
20 | this.a = a;
21 | this.b = b;
22 | this.c = c;
23 | }
24 |
25 | public boolean contains(Point point) {
26 | double pab = point.sub(a).cross(b.sub(a));
27 | double pbc = point.sub(b).cross(c.sub(b));
28 |
29 | if (!MathUtils.hasSameSign(pab, pbc)) {
30 | return false;
31 | }
32 |
33 | double pca = point.sub(c).cross(a.sub(c));
34 |
35 | return MathUtils.hasSameSign(pab, pca);
36 | }
37 |
38 | public boolean isPointInCircumcircle(Point point) {
39 | double a11 = a.x - point.x;
40 | double a21 = b.x - point.x;
41 | double a31 = c.x - point.x;
42 |
43 | double a12 = a.y - point.y;
44 | double a22 = b.y - point.y;
45 | double a32 = c.y - point.y;
46 |
47 | double a13 = (a.x - point.x) * (a.x - point.x) + (a.y - point.y) * (a.y - point.y);
48 | double a23 = (b.x - point.x) * (b.x - point.x) + (b.y - point.y) * (b.y - point.y);
49 | double a33 = (c.x - point.x) * (c.x - point.x) + (c.y - point.y) * (c.y - point.y);
50 |
51 | double det = a11 * a22 * a33 + a12 * a23 * a31 + a13 * a21 * a32 - a13 * a22 * a31
52 | - a12 * a21 * a33 - a11 * a23 * a32;
53 |
54 | if (isOrientedCCW()) {
55 | return det > 0.0d;
56 | }
57 |
58 | return det < 0.0d;
59 | }
60 |
61 | public boolean isOrientedCCW() {
62 | double a11 = a.x - c.x;
63 | double a21 = b.x - c.x;
64 |
65 | double a12 = a.y - c.y;
66 | double a22 = b.y - c.y;
67 |
68 | double det = a11 * a22 - a12 * a21;
69 |
70 | return det > 0.0d;
71 | }
72 |
73 | public boolean isNeighbour(Edge edge) {
74 | return (a == edge.a || b == edge.a || c == edge.a)
75 | && (a == edge.b || b == edge.b || c == edge.b);
76 | }
77 |
78 | public Point getNoneEdgeVertex(Edge edge) {
79 | if (a != edge.a && a != edge.b) {
80 | return a;
81 | } else if (b != edge.a && b != edge.b) {
82 | return b;
83 | } else if (c != edge.a && c != edge.b) {
84 | return c;
85 | }
86 | return null;
87 | }
88 |
89 | public boolean hasVertex(Point point) {
90 | return a == point || b == point || c == point;
91 | }
92 |
93 | public EdgeDistanceComparator findNearestEdge(Point point) {
94 | EdgeDistanceComparator[] edges = new EdgeDistanceComparator[3];
95 |
96 | edges[0] = new EdgeDistanceComparator(
97 | new Edge(a, b), computeClosestPoint(new Edge(a, b), point).sub(point).mag());
98 | edges[1] = new EdgeDistanceComparator(
99 | new Edge(b, c), computeClosestPoint(new Edge(b, c), point).sub(point).mag());
100 | edges[2] = new EdgeDistanceComparator(
101 | new Edge(c, a), computeClosestPoint(new Edge(c, a), point).sub(point).mag());
102 |
103 | Arrays.sort(edges);
104 | return edges[0];
105 | }
106 |
107 | private Point computeClosestPoint(Edge edge, Point point) {
108 | Point ab = edge.b.sub(edge.a);
109 | double t = point.sub(edge.a).dot(ab) / ab.dot(ab);
110 |
111 | if (t < 0.0d) {
112 | t = 0.0d;
113 | } else if (t > 1.0d) {
114 | t = 1.0d;
115 | }
116 |
117 | return edge.a.add(ab.mult((int) t));
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/generator/color/ColorGenerator.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.generator.color;
2 |
3 | /**
4 | * Color generator
5 | *
6 | * @author manolovn
7 | */
8 | public interface ColorGenerator {
9 |
10 | int nextColor();
11 |
12 | void setCount(int count);
13 | }
14 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/generator/color/RandomColorGenerator.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.generator.color;
2 |
3 | import android.graphics.Color;
4 |
5 | import java.util.Random;
6 |
7 | /**
8 | * Random color generator
9 | *
10 | * @author manolovn
11 | */
12 | public class RandomColorGenerator implements ColorGenerator {
13 |
14 | private Random rand = new Random();
15 |
16 | public RandomColorGenerator() {
17 |
18 | }
19 |
20 | @Override
21 | public int nextColor() {
22 | int r = rand.nextInt(255);
23 | int g = rand.nextInt(255);
24 | int b = rand.nextInt(255);
25 |
26 | return Color.rgb(r, g, b);
27 | }
28 |
29 | @Override
30 | public void setCount(int count) {
31 | // not necessary
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/generator/point/CircularPointGenerator.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.generator.point;
2 |
3 | import com.manolovn.trianglify.domain.Point;
4 |
5 | import java.util.Random;
6 | import java.util.Vector;
7 |
8 | /**
9 | * Circular point generator
10 | *
11 | * @author manolovn
12 | */
13 | public class CircularPointGenerator implements PointGenerator {
14 |
15 | private final static int POINTS_PER_CIRCLE = 8;
16 | private final Random random = new Random();
17 |
18 | private int bleedX = 0;
19 | private int bleedY = 0;
20 |
21 | public CircularPointGenerator() {
22 |
23 | }
24 |
25 | @Override
26 | public Vector generatePoints(int width, int height, int cellSize, int variance) {
27 | Vector points = new Vector<>();
28 |
29 | Point center = new Point(width / 2, height / 2);
30 | points.add(new Point(center.x, center.y));
31 |
32 | int limit = Math.max(width + bleedX, height + bleedY);
33 |
34 | for (int radius = cellSize; radius < limit; radius += cellSize) {
35 | double slice = 2 * Math.PI / POINTS_PER_CIRCLE;
36 | for (int i = 0; i < POINTS_PER_CIRCLE; i++) {
37 | double angle = slice * i;
38 | int x = (int) (center.x + radius * Math.cos(angle)) + random.nextInt(variance);
39 | int y = (int) (center.y + radius * Math.sin(angle)) + random.nextInt(variance);
40 | points.add(new Point(x, y));
41 | }
42 | }
43 |
44 | return points;
45 | }
46 |
47 | @Override
48 | public void setBleedX(int bleedX) {
49 | this.bleedX = bleedX;
50 | }
51 |
52 | @Override
53 | public void setBleedY(int bleedY) {
54 | this.bleedY = bleedY;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/generator/point/PointGenerator.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.generator.point;
2 |
3 | import com.manolovn.trianglify.domain.Point;
4 |
5 | import java.util.Vector;
6 |
7 | /**
8 | * Point generator
9 | *
10 | * @author manolovn
11 | */
12 | public interface PointGenerator {
13 |
14 | Vector generatePoints(int width, int height, int cellSize, int variance);
15 |
16 | void setBleedX(int bleedX);
17 |
18 | void setBleedY(int bleedY);
19 | }
20 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/generator/point/RegularPointGenerator.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.generator.point;
2 |
3 | import com.manolovn.trianglify.domain.Point;
4 |
5 | import java.util.Random;
6 | import java.util.Vector;
7 |
8 | /**
9 | * Regular point generator
10 | *
11 | * @author manolovn
12 | */
13 | public class RegularPointGenerator implements PointGenerator {
14 |
15 | private final Random random = new Random();
16 |
17 | private int bleedX = 0;
18 | private int bleedY = 0;
19 |
20 | public RegularPointGenerator() {
21 |
22 | }
23 |
24 | @Override
25 | public void setBleedX(int bleedX) {
26 | this.bleedX = bleedX;
27 | }
28 |
29 | @Override
30 | public void setBleedY(int bleedY) {
31 | this.bleedY = bleedY;
32 | }
33 |
34 | @Override
35 | public Vector generatePoints(int width, int height, int cellSize, int variance) {
36 | Vector points = new Vector<>();
37 |
38 | for (int j = -bleedY; j < height + bleedY; j += cellSize) {
39 | for (int i = -bleedX; i < width + bleedX; i += cellSize) {
40 | int x = i + random.nextInt(variance);
41 | int y = j + random.nextInt(variance);
42 | points.add(new Point(x, y));
43 | }
44 | }
45 |
46 | return points;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/renderer/PointRenderer.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.renderer;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Color;
5 | import android.graphics.Paint;
6 |
7 | import com.manolovn.trianglify.domain.Point;
8 |
9 | import java.util.Vector;
10 |
11 | /**
12 | * Point renderer
13 | *
14 | * @author manolovn
15 | */
16 | public class PointRenderer {
17 |
18 | private int radius = 10;
19 | private Paint paint = new Paint();
20 |
21 | public PointRenderer() {
22 | paint.setStyle(Paint.Style.FILL);
23 | paint.setAntiAlias(true);
24 | paint.setStrokeWidth(0);
25 | paint.setColor(Color.argb(125, 255, 255, 255));
26 | }
27 |
28 | public void render(Vector points, Canvas canvas) {
29 | for (Point point : points) {
30 | canvas.drawCircle(point.x, point.y, radius, paint);
31 | }
32 | }
33 | }
34 |
35 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/renderer/TriangleRenderer.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.renderer;
2 |
3 | import android.graphics.Canvas;
4 | import android.graphics.Paint;
5 | import android.graphics.Path;
6 |
7 | import com.manolovn.trianglify.domain.Triangle;
8 | import com.manolovn.trianglify.generator.color.ColorGenerator;
9 | import com.manolovn.trianglify.generator.color.RandomColorGenerator;
10 |
11 | import java.util.Collection;
12 |
13 | /**
14 | * Triangle renderer
15 | *
16 | * @author manolovn
17 | */
18 | public class TriangleRenderer {
19 |
20 | private Paint trianglePaint;
21 | private ColorGenerator colorGenerator;
22 |
23 | private Path path;
24 |
25 | public TriangleRenderer(ColorGenerator colorGenerator) {
26 | this.colorGenerator = colorGenerator;
27 |
28 | initPaint();
29 | }
30 |
31 | private void initPaint() {
32 | trianglePaint = new Paint();
33 | trianglePaint.setStyle(Paint.Style.FILL);
34 | trianglePaint.setAntiAlias(true);
35 | trianglePaint.setStrokeWidth(2);
36 | trianglePaint.setStrokeCap(Paint.Cap.SQUARE);
37 | trianglePaint.setStrokeJoin(Paint.Join.BEVEL);
38 |
39 | if (colorGenerator == null) {
40 | colorGenerator = new RandomColorGenerator();
41 | }
42 |
43 | path = new Path();
44 | path.setFillType(Path.FillType.EVEN_ODD);
45 | }
46 |
47 | public void render(Collection triangles, Canvas canvas) {
48 | colorGenerator.setCount(triangles.size());
49 | for (Triangle triangle : triangles) {
50 | path.reset();
51 |
52 | path.moveTo(triangle.a.x, triangle.a.y);
53 | path.lineTo(triangle.b.x, triangle.b.y);
54 | path.lineTo(triangle.c.x, triangle.c.y);
55 | path.lineTo(triangle.a.x, triangle.a.y);
56 |
57 | path.close();
58 |
59 | int color = colorGenerator.nextColor();
60 |
61 | trianglePaint.setColor(color);
62 | canvas.drawPath(path, trianglePaint);
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/triangulator/DelaunayTriangulator.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.triangulator;
2 |
3 | import com.manolovn.trianglify.domain.Edge;
4 | import com.manolovn.trianglify.domain.Point;
5 | import com.manolovn.trianglify.domain.Triangle;
6 | import com.manolovn.trianglify.util.Preconditions;
7 |
8 | import java.util.Collection;
9 | import java.util.Vector;
10 |
11 | /**
12 | * Delaunay triangulator
13 | *
14 | * @author manolovn
15 | */
16 | public class DelaunayTriangulator implements Triangulator {
17 |
18 | private Triangulation triangulation;
19 |
20 | public DelaunayTriangulator() {
21 |
22 | }
23 |
24 | @Override
25 | public Vector triangulate(Vector pointSet) {
26 | Preconditions.checkNotNull(pointSet);
27 | Preconditions.checkArgument(pointSet.size() >= 3, "Can't triangulate less than 3 points");
28 | triangulation = new Triangulation();
29 |
30 | Triangle superTriangle = generateSuperTriangle(pointSet);
31 | triangulation.add(superTriangle);
32 |
33 | for (int i = 0; i < pointSet.size(); i++) {
34 | Triangle triangle = triangulation.findContainingTriangle(pointSet.get(i));
35 |
36 | if (triangle == null) {
37 | Edge edge = triangulation.findNearestEdge(pointSet.get(i));
38 |
39 | Triangle first = triangulation.findOneTriangleSharing(edge);
40 | Triangle second = triangulation.findNeighbour(first, edge);
41 |
42 | if (first == null || second == null) {
43 | continue;
44 | }
45 |
46 | Point firstNoneEdgeVertex = first.getNoneEdgeVertex(edge);
47 | Point secondNoneEdgeVertex = second.getNoneEdgeVertex(edge);
48 |
49 | triangulation.remove(first);
50 | triangulation.remove(second);
51 |
52 | Triangle triangle1 = new Triangle(edge.a, firstNoneEdgeVertex, pointSet.get(i));
53 | Triangle triangle2 = new Triangle(edge.b, firstNoneEdgeVertex, pointSet.get(i));
54 | Triangle triangle3 = new Triangle(edge.a, secondNoneEdgeVertex, pointSet.get(i));
55 | Triangle triangle4 = new Triangle(edge.b, secondNoneEdgeVertex, pointSet.get(i));
56 |
57 | triangulation.add(triangle1);
58 | triangulation.add(triangle2);
59 | triangulation.add(triangle3);
60 | triangulation.add(triangle4);
61 |
62 | legalizeEdge(triangle1, new Edge(edge.a, firstNoneEdgeVertex), pointSet.get(i));
63 | legalizeEdge(triangle2, new Edge(edge.b, firstNoneEdgeVertex), pointSet.get(i));
64 | legalizeEdge(triangle3, new Edge(edge.a, secondNoneEdgeVertex), pointSet.get(i));
65 | legalizeEdge(triangle4, new Edge(edge.b, secondNoneEdgeVertex), pointSet.get(i));
66 | } else {
67 | Point a = triangle.a;
68 | Point b = triangle.b;
69 | Point c = triangle.c;
70 |
71 | triangulation.remove(triangle);
72 |
73 | Triangle first = new Triangle(a, b, pointSet.get(i));
74 | Triangle second = new Triangle(b, c, pointSet.get(i));
75 | Triangle third = new Triangle(c, a, pointSet.get(i));
76 |
77 | triangulation.add(first);
78 | triangulation.add(second);
79 | triangulation.add(third);
80 |
81 | legalizeEdge(first, new Edge(a, b), pointSet.get(i));
82 | legalizeEdge(second, new Edge(b, c), pointSet.get(i));
83 | legalizeEdge(third, new Edge(c, a), pointSet.get(i));
84 | }
85 | }
86 |
87 | // remove super triangle
88 | triangulation.removeTrianglesUsing(superTriangle.a);
89 | triangulation.removeTrianglesUsing(superTriangle.b);
90 | triangulation.removeTrianglesUsing(superTriangle.c);
91 |
92 | return triangulation.getTriangles();
93 | }
94 |
95 | private void legalizeEdge(Triangle triangle, Edge edge, Point vertex) {
96 | Triangle neighbourTriangle = triangulation.findNeighbour(triangle, edge);
97 |
98 | if (neighbourTriangle != null) {
99 | if (neighbourTriangle.isPointInCircumcircle(vertex)) {
100 | triangulation.remove(triangle);
101 | triangulation.remove(neighbourTriangle);
102 |
103 | Point noneEdgeVertex = neighbourTriangle.getNoneEdgeVertex(edge);
104 |
105 | Triangle firstTriangle = new Triangle(noneEdgeVertex, edge.a, vertex);
106 | Triangle secondTriangle = new Triangle(noneEdgeVertex, edge.b, vertex);
107 |
108 | triangulation.add(firstTriangle);
109 | triangulation.add(secondTriangle);
110 |
111 | legalizeEdge(firstTriangle, new Edge(noneEdgeVertex, edge.a), vertex);
112 | legalizeEdge(secondTriangle, new Edge(noneEdgeVertex, edge.b), vertex);
113 | }
114 | }
115 | }
116 |
117 | private Triangle generateSuperTriangle(Collection pointSet) {
118 | final int factor = 3;
119 | int maxCoordinate = 0;
120 | int minCoordinate = 0;
121 |
122 | for (Point point : pointSet) {
123 | maxCoordinate = Math.max(Math.max(point.x, point.y), maxCoordinate);
124 | minCoordinate = Math.min(Math.min(point.x, point.y), minCoordinate);
125 | }
126 |
127 | Point p1 = new Point(minCoordinate, factor * maxCoordinate);
128 | Point p2 = new Point(factor * maxCoordinate, minCoordinate);
129 | Point p3 = new Point(-factor * maxCoordinate, -factor * maxCoordinate);
130 |
131 | return new Triangle(p1, p2, p3);
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/triangulator/Triangulation.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.triangulator;
2 |
3 | import com.manolovn.trianglify.domain.Point;
4 | import com.manolovn.trianglify.domain.Edge;
5 | import com.manolovn.trianglify.domain.Triangle;
6 | import com.manolovn.trianglify.util.EdgeDistanceComparator;
7 |
8 | import java.util.Arrays;
9 | import java.util.Vector;
10 |
11 | /**
12 | * Triangulation model
13 | *
14 | * @author manolovn
15 | */
16 | public class Triangulation {
17 |
18 | private Vector triangleVector;
19 |
20 | public Triangulation() {
21 | this.triangleVector = new Vector<>();
22 | }
23 |
24 | public void add(Triangle triangle) {
25 | this.triangleVector.add(triangle);
26 | }
27 |
28 | public void remove(Triangle triangle) {
29 | this.triangleVector.remove(triangle);
30 | }
31 |
32 | public Vector getTriangles() {
33 | return this.triangleVector;
34 | }
35 |
36 | public Triangle findContainingTriangle(Point point) {
37 | for (Triangle triangle : triangleVector) {
38 | if (triangle.contains(point)) {
39 | return triangle;
40 | }
41 | }
42 | return null;
43 | }
44 |
45 | public Triangle findNeighbour(Triangle triangle, Edge edge) {
46 | for (Triangle triangleFromSoup : triangleVector) {
47 | if (triangleFromSoup.isNeighbour(edge) && triangleFromSoup != triangle) {
48 | return triangleFromSoup;
49 | }
50 | }
51 | return null;
52 | }
53 |
54 | public Triangle findOneTriangleSharing(Edge edge) {
55 | for (Triangle triangle : triangleVector) {
56 | if (triangle.isNeighbour(edge)) {
57 | return triangle;
58 | }
59 | }
60 | return null;
61 | }
62 |
63 | public Edge findNearestEdge(Point point) {
64 | Vector edgeVector = new Vector<>();
65 |
66 | for (Triangle triangle : triangleVector) {
67 | edgeVector.add(triangle.findNearestEdge(point));
68 | }
69 |
70 | EdgeDistanceComparator[] edgeDistancePacks = new EdgeDistanceComparator[edgeVector.size()];
71 | edgeVector.toArray(edgeDistancePacks);
72 |
73 | Arrays.sort(edgeDistancePacks);
74 | return edgeDistancePacks[0].edge;
75 | }
76 |
77 | public void removeTrianglesUsing(Point vertex) {
78 | Vector trianglesToBeRemoved = new Vector<>();
79 |
80 | for (Triangle triangle : triangleVector) {
81 | if (triangle.hasVertex(vertex)) {
82 | trianglesToBeRemoved.add(triangle);
83 | }
84 | }
85 |
86 | triangleVector.removeAll(trianglesToBeRemoved);
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/triangulator/Triangulator.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.triangulator;
2 |
3 | import com.manolovn.trianglify.domain.Point;
4 | import com.manolovn.trianglify.domain.Triangle;
5 |
6 | import java.util.Vector;
7 |
8 | /**
9 | * Triangulator behavior
10 | *
11 | * @author manolovn
12 | */
13 | public interface Triangulator {
14 |
15 | Vector triangulate(Vector pointSet);
16 | }
17 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/util/ClassUtil.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.util;
2 |
3 | /**
4 | * Utility methods
5 | *
6 | * @author manolovn
7 | */
8 | public class ClassUtil {
9 |
10 | public static T getClassByName(String className, Class classOfT) {
11 | try {
12 | Class> clazz = Class.forName(className);
13 | Object instance = clazz.newInstance();
14 | if (classOfT.isInstance(instance)) {
15 | return classOfT.cast(instance);
16 | }
17 | } catch (Exception e) {
18 | e.printStackTrace();
19 | }
20 | return null;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/util/EdgeDistanceComparator.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.util;
2 |
3 | import android.support.annotation.NonNull;
4 |
5 | import com.manolovn.trianglify.domain.Edge;
6 |
7 | /**
8 | * Edge distance comparator
9 | *
10 | * @author manolovn
11 | */
12 | public class EdgeDistanceComparator implements Comparable {
13 |
14 | public Edge edge;
15 | public double distance;
16 |
17 | public EdgeDistanceComparator(Edge edge, double distance) {
18 | this.edge = edge;
19 | this.distance = distance;
20 | }
21 |
22 | @Override
23 | public int compareTo(@NonNull EdgeDistanceComparator o) {
24 | if (o.distance == distance) {
25 | return 0;
26 | } else if (o.distance < distance) {
27 | return 1;
28 | } else {
29 | return -1;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/util/MathUtils.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.util;
2 |
3 | /**
4 | * Math utils
5 | *
6 | * @author manolovn
7 | */
8 | public class MathUtils {
9 |
10 | public static boolean hasSameSign(double a, double b) {
11 | return Math.signum(a) == Math.signum(b);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/trianglify/src/main/java/com/manolovn/trianglify/util/Preconditions.java:
--------------------------------------------------------------------------------
1 | package com.manolovn.trianglify.util;
2 |
3 | import android.support.annotation.Nullable;
4 |
5 | /**
6 | * Preconditions (extracted from guava)
7 | *
8 | * @author manolovn
9 | */
10 | public class Preconditions {
11 |
12 | private Preconditions() {
13 |
14 | }
15 |
16 | public static T checkNotNull(T reference) {
17 | if (reference == null) {
18 | throw new NullPointerException();
19 | }
20 | return reference;
21 | }
22 |
23 | public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
24 | if (!expression) {
25 | throw new IllegalArgumentException(String.valueOf(errorMessage));
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/trianglify/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/trianglify/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Trianglify
3 |
4 |
--------------------------------------------------------------------------------