Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (707)
Games in Android Showcase (206)
games submitted by our members
Games in WIP (781)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: theagentd (posted 2016-10-24 17:51:53, viewed 1207 times)

1   2   3   4   5   6   7   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   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168  
package engine.test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DOFSampleLocationGenerator3 {
   
   public static void main(String[] args) {
      
      final double spirals = 12;
      final int steps = 128 * 1024;
      final int numSamples = 128;

      double prevX = 0, prevY = 0;
      double totalLength = 0;
      for(int i = 0; i <= steps; i++){
         
         double alpha = (i+0.5) / steps;
         double a = Math.PI * 2 * spirals * alpha;
         
         double radius = alpha;
         
         double x = Math.cos(a) * radius, y = Math.sin(a) * radius;
         
         double dx = x - prevX, dy = y - prevY;
         double l = Math.sqrt(dx*dx + dy*dy);
         
         totalLength += l;
         
         prevX = x;
         prevY = y;
      }
      
      
      double sampleSpacing = totalLength / (numSamples - 1);
      
      ArrayList<Sample> sampleList = new ArrayList<>();
      
      sampleList.add(new Sample(0, 0));
      
      prevX = 0;
      prevY = 0;
      double length = 0;
      for(int i = 0; i <= steps; i++){
         
         double alpha = (i+0.5) / steps;
         double a = Math.PI * 2 * spirals * alpha;
         
         double radius = alpha;
         
         double x = Math.cos(a) * radius, y = Math.sin(a) * radius;
         
         double dx = x - prevX, dy = y - prevY;
         double l = Math.sqrt(dx*dx + dy*dy);
         
         length += l / sampleSpacing;
         if(length > 1){
            length--;
            sampleList.add(new Sample(x, y));
            /*if(sampleList.size() == numSamples){
               break;
            }*/
         }
         
         prevX = x;
         prevY = y;
      }
      
      final int samples = sampleList.size();
      final double[] sampleX = new double[samples];
      final double[] sampleY = new double[samples];
      
      System.out.print("vec2 offsets[" + samples + "] = vec2[" + samples + "](");
      
      for(int i = 0; i < samples; i++){
         
         Sample s = sampleList.get(i);
         double x = s.x, y = s.y;
         
         if(Math.abs(x) - 0.00001 <= 0){
            x = 0;
         }
         
         if(Math.abs(y) - 0.00001 <= 0){
            y = 0;
         }
         
         sampleX[i] = x;
         sampleY[i] = y;
         
         System.out.print("vec2(" + (float)x + ", " + (float)y + ")");
         if(i != samples-1){
            System.out.print(", ");
         }
      }
      System.out.println(");");
      
      JFrame f = new JFrame("Sample locations");
      f.setLocationRelativeTo(null);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().add(new JPanel(){
         
         private static final long serialVersionUID = 1;

         {
            setPreferredSize(new Dimension(512, 512));
         }
         
         @Override
         public void paint(Graphics g) {
            super.paint(g);
            
            g.setColor(Color.red);
            g.drawOval(0, 0, 512-1, 512-1);

            g.setColor(new Color(220, 220, 220));
            double prevX = 0, prevY = 0;
            for(int i = 0; i <= steps; i++){
               
               double alpha = (i+0.5) / steps;
               double a = Math.PI * 2 * spirals * alpha;
               
               double radius = alpha;
               
               double x = Math.cos(a) * radius, y = Math.sin(a) * radius;
               
               double dx = x - prevX, dy = y - prevY;
               double l = Math.sqrt(dx*dx + dy*dy);
               
               g.drawLine((int)Math.round((prevX*0.5 + 0.5) * 512), (int)Math.round((prevY*0.5 + 0.5) * 512), (int)Math.round((x*0.5 + 0.5) * 512), (int)Math.round((y*0.5 + 0.5) * 512));
               
               prevX = x;
               prevY = y;
            }

            g.setColor(Color.red);
            for(int i = 0; i < samples; i++){
               
               double sx = sampleX[i], sy = sampleY[i];

               int x = (int)Math.round((sx*0.5 + 0.5) * 512);
               int y = (int)Math.round((sy*0.5 + 0.5) * 512);
               
               g.fillRect(x-5, y-5, 10, 10);
            }
         }
      });
      f.pack();
      
      f.setVisible(true);
      
      
   }
   
   private static class Sample{
      double x, y;
      
      public Sample(double x, double y) {
         this.x = x;
         this.y = y;
      }
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
Galdo (228 views)
2017-01-12 13:44:09

Archive (397 views)
2017-01-02 05:31:41

0AndrewShepherd0 (855 views)
2016-12-16 03:58:39

0AndrewShepherd0 (792 views)
2016-12-15 21:50:57

Lunch (930 views)
2016-12-06 16:01:40

ral0r2 (1157 views)
2016-11-23 16:08:26

ClaasJG (1263 views)
2016-11-10 17:36:32

CoffeeChemist (1303 views)
2016-11-05 00:46:53

jay4842 (1386 views)
2016-11-01 19:04:52

theagentd (1206 views)
2016-10-24 17:51:53
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!