001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.anim.values;
020:
021: import org.apache.batik.dom.anim.AnimationTarget;
022:
023: /**
024: * A point value in the animation system from a motion animation.
025: *
026: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
027: * @version $Id: AnimatableMotionPointValue.java 475477 2006-11-15 22:44:28Z cam $
028: */
029: public class AnimatableMotionPointValue extends AnimatableValue {
030:
031: /**
032: * The x coordinate.
033: */
034: protected float x;
035:
036: /**
037: * The y coordinate.
038: */
039: protected float y;
040:
041: /**
042: * The rotation angle in radians.
043: */
044: protected float angle;
045:
046: /**
047: * Creates a new, uninitialized AnimatableMotionPointValue.
048: */
049: protected AnimatableMotionPointValue(AnimationTarget target) {
050: super (target);
051: }
052:
053: /**
054: * Creates a new AnimatableMotionPointValue with one x.
055: */
056: public AnimatableMotionPointValue(AnimationTarget target, float x,
057: float y, float angle) {
058: super (target);
059: this .x = x;
060: this .y = y;
061: this .angle = angle;
062: }
063:
064: /**
065: * Performs interpolation to the given value.
066: */
067: public AnimatableValue interpolate(AnimatableValue result,
068: AnimatableValue to, float interpolation,
069: AnimatableValue accumulation, int multiplier) {
070: AnimatableMotionPointValue res;
071: if (result == null) {
072: res = new AnimatableMotionPointValue(target);
073: } else {
074: res = (AnimatableMotionPointValue) result;
075: }
076:
077: float newX = x, newY = y, newAngle = angle;
078: int angleCount = 1;
079:
080: if (to != null) {
081: AnimatableMotionPointValue toValue = (AnimatableMotionPointValue) to;
082: newX += interpolation * (toValue.x - x);
083: newY += interpolation * (toValue.y - y);
084: newAngle += toValue.angle;
085: angleCount++;
086: }
087: if (accumulation != null && multiplier != 0) {
088: AnimatableMotionPointValue accValue = (AnimatableMotionPointValue) accumulation;
089: newX += multiplier * accValue.x;
090: newY += multiplier * accValue.y;
091: newAngle += accValue.angle;
092: angleCount++;
093: }
094: newAngle /= angleCount;
095:
096: if (res.x != newX || res.y != newY || res.angle != newAngle) {
097: res.x = newX;
098: res.y = newY;
099: res.angle = newAngle;
100: res.hasChanged = true;
101: }
102: return res;
103: }
104:
105: /**
106: * Returns the x coordinate.
107: */
108: public float getX() {
109: return x;
110: }
111:
112: /**
113: * Returns the y coordinate.
114: */
115: public float getY() {
116: return y;
117: }
118:
119: /**
120: * Returns the rotation angle.
121: */
122: public float getAngle() {
123: return angle;
124: }
125:
126: /**
127: * Returns whether two values of this type can have their distance
128: * computed, as needed by paced animation.
129: */
130: public boolean canPace() {
131: return true;
132: }
133:
134: /**
135: * Returns the absolute distance between this value and the specified other
136: * value.
137: */
138: public float distanceTo(AnimatableValue other) {
139: AnimatableMotionPointValue o = (AnimatableMotionPointValue) other;
140: float dx = x - o.x;
141: float dy = y - o.y;
142: return (float) Math.sqrt(dx * dx + dy * dy);
143: }
144:
145: /**
146: * Returns a zero value of this AnimatableValue's type.
147: */
148: public AnimatableValue getZeroValue() {
149: return new AnimatableMotionPointValue(target, 0f, 0f, 0f);
150: }
151:
152: /**
153: * Returns a string representation of this object.
154: */
155: public String toStringRep() {
156: StringBuffer sb = new StringBuffer();
157: sb.append(formatNumber(x));
158: sb.append(',');
159: sb.append(formatNumber(y));
160: sb.append(',');
161: sb.append(formatNumber(angle));
162: sb.append("rad");
163: return sb.toString();
164: }
165: }
|