Android Open Source - adventure.datetime Node Placer






From Project

Back to project page adventure.datetime.

License

The source code is released under:

MIT License

If you think the Android project adventure.datetime listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ca.cmput301f13t03.adventure_datetime.view.treeView;
// w  ww .j  av  a2 s .c o  m
import ca.cmput301f13t03.adventure_datetime.model.Choice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class NodePlacer
{
  private static final int SEGMENT_SIZE = 800;
  private static final float PLACEMENT_SCALE = 1.75f; // bigger numbers will place the nodes further
  
  ArrayList<GridSegment> m_gridSegments = new ArrayList<GridSegment>();
  Map<String, FragmentNode> m_placedNodes = new HashMap<String, FragmentNode>();
  
  NodePlacer()
  {
    
  }
  
  public void PlaceFragment(FragmentNode fragment)
  {
    // placement is judged by weighting this nodes position against all of the
    // nodes it is linked too and then finding a spot that it fits that is near where
    // we started
    
    List<FragmentNode> linkedFrags = GetLinkedFrags(fragment);
    
    int centerX = 0;
    int centerY = 0;
    
    for(FragmentNode node : linkedFrags)
    {
      centerX += node.x;
      centerY += node.y;
    }
    
    if(linkedFrags.size() > 0)
    {
      centerX /= linkedFrags.size();
      centerY /= linkedFrags.size();
    }
    
    PlaceNear(centerX, centerY, fragment);
  }
  
  private void PlaceNear(int x, int y, FragmentNode fragment)
  {
    boolean isPlaced = false;
    
    // first try placing at the original point, if not we'll expand our search outwards
    fragment.x = x;
    fragment.y = y;
    isPlaced = TryPlace(fragment);
    
    if(!isPlaced)
    {
      // then we failed to place at the desired point
      // search outwards by trying locations and stopping
      // after a success
      final double EXPECTED_RADIUS = fragment.width * PLACEMENT_SCALE;
      final double VERTICAL_MOD = (double)fragment.height / (double)fragment.width;
      final double HORIZONTAL_MOD = (double)fragment.width / (double)fragment.width; // kinda reduntant...
      
      // select a random 45 degree angle
      double angle = (Math.random() * 7);
      angle *= 45;
      double radiusModifier = 1.0;
      
      angle = Math.toRadians(angle);
      
      double angleIncrement = Math.toRadians(45);
      double twoPi = Math.toRadians(360.0);
      double INITIAL_ANGLE = angle;
      
      while(!isPlaced)
      {
        fragment.x = (int) (Math.cos(angle) * EXPECTED_RADIUS * HORIZONTAL_MOD * radiusModifier) + x;
        fragment.y = (int) (Math.sin(angle) * EXPECTED_RADIUS * VERTICAL_MOD * radiusModifier) + y;
        isPlaced = TryPlace(fragment);
        
        angle += angleIncrement;
        if(angle - INITIAL_ANGLE > twoPi)
        {
          angle -= twoPi;
          radiusModifier++;
        }
      }
    }
  }
  
  private boolean TryPlace(FragmentNode fragment)
  {
    GridSegment gridSegment = GetGridSegmentAt(fragment.x, fragment.y);
    
    if(gridSegment.CanPlace(fragment))
    {
      gridSegment.Place(fragment);
      return true;
    }
    else
    {
      return false;
    }
  }
  
  private GridSegment GetGridSegmentAt(int x, int y)
  {
    GridSegment result = null;
    for(GridSegment segment : m_gridSegments)
    {
      if(segment.Contains(x, y))
      {
        result = segment;
      }
    }
    
    if(result == null)
    {
      // no segment is at that position yet
      // so we'll make one!
      
      // round x down to the nearest SEGMENT_SIZE
      // down is defined as closer to negative infinity
      int newX = (int) Math.floor(x / (float)SEGMENT_SIZE) * SEGMENT_SIZE;
      int newY = (int) Math.floor(y / (float)SEGMENT_SIZE) * SEGMENT_SIZE;
      
      result = new GridSegment(newX, newY, SEGMENT_SIZE, SEGMENT_SIZE);
      m_gridSegments.add(result);
    }
    
    return result;
  }
  
  private List<FragmentNode> GetLinkedFrags(FragmentNode fragment)
  {
    List<FragmentNode> result = new ArrayList<FragmentNode>();
    
    for(Choice adjacentIds : fragment.GetFragment().getChoices())
    {
      if(m_placedNodes.containsKey(adjacentIds.getTarget()))
      {
        result.add(m_placedNodes.get(adjacentIds.getTarget()));
      }
    }
    
    return result;
  }
  
  public ArrayList<GridSegment> GetSegments()
  {
    return m_gridSegments;
  }
}




Java Source Code List

ca.cmput301f13t03.adventure_datetime.controller.AuthorControllerTest.java
ca.cmput301f13t03.adventure_datetime.controller.AuthorController.java
ca.cmput301f13t03.adventure_datetime.controller.UserControllerTest.java
ca.cmput301f13t03.adventure_datetime.controller.UserController.java
ca.cmput301f13t03.adventure_datetime.model.AccountServiceTests.java
ca.cmput301f13t03.adventure_datetime.model.AccountService.java
ca.cmput301f13t03.adventure_datetime.model.BookmarkTest.java
ca.cmput301f13t03.adventure_datetime.model.Bookmark.java
ca.cmput301f13t03.adventure_datetime.model.ChoiceTest.java
ca.cmput301f13t03.adventure_datetime.model.Choice.java
ca.cmput301f13t03.adventure_datetime.model.CommentTest.java
ca.cmput301f13t03.adventure_datetime.model.Comment.java
ca.cmput301f13t03.adventure_datetime.model.ES.java
ca.cmput301f13t03.adventure_datetime.model.ImageTest.java
ca.cmput301f13t03.adventure_datetime.model.Image.java
ca.cmput301f13t03.adventure_datetime.model.StoryDBTest.java
ca.cmput301f13t03.adventure_datetime.model.StoryDB.java
ca.cmput301f13t03.adventure_datetime.model.StoryFragmentTest.java
ca.cmput301f13t03.adventure_datetime.model.StoryFragment.java
ca.cmput301f13t03.adventure_datetime.model.StoryManager.java
ca.cmput301f13t03.adventure_datetime.model.StoryTest.java
ca.cmput301f13t03.adventure_datetime.model.Story.java
ca.cmput301f13t03.adventure_datetime.model.ThreadPool.java
ca.cmput301f13t03.adventure_datetime.model.WebStorageCleaner.java
ca.cmput301f13t03.adventure_datetime.model.WebStorageTest.java
ca.cmput301f13t03.adventure_datetime.model.WebStorage.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IAllFragmentsListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IBookmarkListListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ICommentsListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ICurrentFragmentListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ICurrentStoryListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ILocalStorage.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ILocalStoriesListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IOnlineStoriesListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IStoryModelDirector.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IStoryModelPresenter.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IWebStorage.java
ca.cmput301f13t03.adventure_datetime.serviceLocator.Locator.java
ca.cmput301f13t03.adventure_datetime.view.AuthorEdit_Edit.java
ca.cmput301f13t03.adventure_datetime.view.AuthorEdit_Overview.java
ca.cmput301f13t03.adventure_datetime.view.AuthorEdit.java
ca.cmput301f13t03.adventure_datetime.view.AuthorStories.java
ca.cmput301f13t03.adventure_datetime.view.AuthorStoryDescription.java
ca.cmput301f13t03.adventure_datetime.view.AuthorViewPager.java
ca.cmput301f13t03.adventure_datetime.view.BrowseFragment.java
ca.cmput301f13t03.adventure_datetime.view.BrowseView.java
ca.cmput301f13t03.adventure_datetime.view.CommentsView.java
ca.cmput301f13t03.adventure_datetime.view.ContinueView.java
ca.cmput301f13t03.adventure_datetime.view.FragmentViewActivity.java
ca.cmput301f13t03.adventure_datetime.view.FragmentView.java
ca.cmput301f13t03.adventure_datetime.view.FullScreen_Image.java
ca.cmput301f13t03.adventure_datetime.view.IFragmentSelected.java
ca.cmput301f13t03.adventure_datetime.view.MainViewTest.java
ca.cmput301f13t03.adventure_datetime.view.MainView.java
ca.cmput301f13t03.adventure_datetime.view.StoryDescription.java
ca.cmput301f13t03.adventure_datetime.view.treeView.Camera.java
ca.cmput301f13t03.adventure_datetime.view.treeView.ConnectionPlacer.java
ca.cmput301f13t03.adventure_datetime.view.treeView.FragmentConnection.java
ca.cmput301f13t03.adventure_datetime.view.treeView.FragmentNode.java
ca.cmput301f13t03.adventure_datetime.view.treeView.GridSegment.java
ca.cmput301f13t03.adventure_datetime.view.treeView.InputHandler.java
ca.cmput301f13t03.adventure_datetime.view.treeView.NodeGrid.java
ca.cmput301f13t03.adventure_datetime.view.treeView.NodePlacer.java
ca.cmput301f13t03.adventure_datetime.view.treeView.Region.java
ca.cmput301f13t03.adventure_datetime.view.treeView.TreeView.java