I'm trying to draw a simple circle when I get to the following line I get the error "Double is Not Convertable to CGFloat under the startAngle = 0.0
path.addArcWithCenter(center, radius: radius, startAngle: 0.0, endAngle: Float(M_PI) * 2.0, clockwise: true)
How do I "cast" 0.0 to make it CGFloat in Swift?
The complete function I am writing:
func drawCircle() {
// Drawing code
var bounds:CGRect = secondView.bounds
var center = CGPoint()
center.x = bounds.origin.x + bounds.size.width / 2.0
center.y = bounds.origin.y + bounds.size.height / 2.0
var radius = (min(bounds.size.width, bounds.size.height) / 2.0)
var path:UIBezierPath = UIBezierPath()
path.addArcWithCenter(center, radius: radius, startAngle: CGFloat(0.0), endAngle: Float(M_PI) * 2.0, clockwise: true)
path.stroke()
}
Convert the values that need to be CGFloat to a CGFloat.
path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(M_PI) * 2.0, clockwise: true)
startAngle probably shouldn't need to be converted though if you're just passing a literal. Also note that this isn't a C style cast, but actually converting between different Swift Types.
Edit: Looking at your whole function, this works.
func drawCircle() {
// Drawing code
var bounds:CGRect = self.view.bounds
var center = CGPoint()
center.x = bounds.origin.x + bounds.size.width / 2.0
center.y = bounds.origin.y + bounds.size.height / 2.0
var radius = (min(bounds.size.width, bounds.size.height) / 2.0)
var path:UIBezierPath = UIBezierPath()
path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(Float(M_PI) * 2.0), clockwise: true)
path.stroke()
}
You must type cast it via CGFloat(0.0). CGFloat has been adjusted to evaluate differently throughout the beta version of Xcode 6 due to the fact that in Obj-C, CGFloat casts to either a float or a double depending on the target (64 bit versus 32 bit). You must type cast a number to CGFloat in Swift to use a CGFloat as you're never guaranteed to have a float or a double (because this is dependent on the environment). This way, Swift won't throw a fit and will still be 'type' safe.
This error will disappear in Swift 5.5.
Maybe it's not a good idea, but I used NSNumber to convert Double to Float, then to CGFloat.
let myFloat = NSNumber.init(value: myDouble).floatValue
let myCGFloat = CGFloat(myFloat)
Related
I have an app that shows a bunch of people who each have an origin and angle.
struct Location {
var centre:CGPoint
var facing:Angle
}
SwiftUI magically and automatically does a lot of the animation as they move from location A to location B
withAnimation {
person.location = newLocation
}
However - for the Angle (facing) property, I want the animation to go in the shortest route (bearing in mind that in the real world - angles wrap around).
e.g. Swift UI correctly animates when the angle changes 5 -> 10 (degrees)
5,6,7,8,9,10
but going from 2 to 358, it takes the long way around
SwiftUI does 2,3,4,5,6,7.......,357,358
where I would like it to do
2,1,0,359,358
how can I go about this?
thank you
update: I'm hoping for a solution which allows me to work with the animation system, perhaps using a new MyAngle struct which provides the animation steps directly, perhaps using some kind of animation modifier.
.easeInOut modifies the steps - is there an equivalent approach where I can create a .goTheRightWay animation?
Ok - Posting my own answer.
It works a bit like #Ben's answer - but moves the 'shadow angle' management to the rotation effect.
All you have to do is switch rotationEffect(angle:Angle) for shortRotationEffect(angle:Angle,id:UUID)
this looks like
#State private var rotationStorage = RotationStorage()
//and then in body
Image(systemName: "person.fill").resizable()
.frame(width: 50, height: 50)
.shortRotationEffect(self.person.angle,id:person.id,storage:rotationStorage)
.animation(.easeInOut)
the ShortRotationEffect uses the provided id to maintain a dictionary of previous angles. When you set a new angle, it figures out the equivalent angle which provides a short rotation and applies that with a normal rotationEffect(...)
Here it is:
class RotationStorage {
private var storage: [UUID: Angle] = [:]
fileprivate func setAngle(id:UUID,angle:Angle) {
storage[id] = angle
}
fileprivate func getAngle(_ id:UUID) -> Angle? {
return storage[id]
}
}
extension View {
/// Like RotationEffect - but when animated, the rotation moves in the shortest direction.
/// - Parameters:
/// - angle: new angle
/// - anchor: anchor point
/// - id: unique id for the item being displayed. This is used as a key to maintain the rotation history and figure out the right direction to move
func shortRotationEffect(_ angle: Angle,
anchor: UnitPoint = .center,
id: UUID,
storage:RotationStorage) -> some View {
modifier(ShortRotation(angle: angle,
anchor: anchor,
id: id,
storage:storage))
}
}
struct ShortRotation: ViewModifier {
var angle: Angle
var anchor: UnitPoint
var id: UUID
let storage:RotationStorage
func getAngle() -> Angle {
var newAngle = angle
if let lastAngle = storage.getAngle(id) {
let change: Double = (newAngle.degrees - lastAngle.degrees) %% 360.double
if change < 180 {
newAngle = lastAngle + Angle.init(degrees: change)
} else {
newAngle = lastAngle + Angle.init(degrees: change - 360)
}
}
storage.setAngle(id: id, angle: newAngle)
return newAngle
}
func body(content: Content) -> some View {
content
.rotationEffect(getAngle(), anchor: anchor)
}
}
this relies on my positive modulus function:
public extension Double {
/// Returns modulus, but forces it to be positive
/// - Parameters:
/// - left: number
/// - right: modulus
/// - Returns: positive modulus
static func %% (_ left: Double, _ right: Double) -> Double {
let truncatingRemainder = left.truncatingRemainder(dividingBy: right)
return truncatingRemainder >= 0 ? truncatingRemainder : truncatingRemainder+abs(right)
}
}
How about adjusting the newLocation value to keep within 180˚ of the start? Here's a function to check if the distance animated is greater than half way around and provide a new endpoint that satisfies it.
func adjustedEnd(from start: CGFloat, to target: CGFloat) -> CGFloat {
// Shift end to be greater than start
var end = target
while end < start { end += 360 }
// Mod the distance with 360, shifting by 180 to keep on the same side of a circle
return (end - start + 180).truncatingRemainder(dividingBy: 360) - 180 + start
}
Some sample test cases:
let startValues: [CGFloat] = [2, -10, 345, 365, 700]
let endValues: [CGFloat] = [2, 10, 180, 185, 350, -10, 715, -700]
for start in startValues {
print("From \(start):")
for end in endValues {
let adjusted = adjustedEnd(from: start, to: end)
print("\t\(end) \tbecomes \(adjusted);\tdistance \(abs(adjusted - start))")
}
}
prints the following:
From 2.0:
2.0 becomes 2.0; distance 0.0
10.0 becomes 10.0; distance 8.0
180.0 becomes 180.0; distance 178.0
185.0 becomes -175.0; distance 177.0
350.0 becomes -10.0; distance 12.0
-10.0 becomes -10.0; distance 12.0
715.0 becomes -5.0; distance 7.0
-700.0 becomes 20.0; distance 18.0
From -10.0:
2.0 becomes 2.0; distance 12.0
10.0 becomes 10.0; distance 20.0
180.0 becomes -180.0; distance 170.0
185.0 becomes -175.0; distance 165.0
350.0 becomes -10.0; distance 0.0
-10.0 becomes -10.0; distance 0.0
715.0 becomes -5.0; distance 5.0
-700.0 becomes 20.0; distance 30.0
From 345.0:
2.0 becomes 362.0; distance 17.0
10.0 becomes 370.0; distance 25.0
180.0 becomes 180.0; distance 165.0
185.0 becomes 185.0; distance 160.0
350.0 becomes 350.0; distance 5.0
-10.0 becomes 350.0; distance 5.0
715.0 becomes 355.0; distance 10.0
-700.0 becomes 380.0; distance 35.0
From 365.0:
2.0 becomes 362.0; distance 3.0
10.0 becomes 370.0; distance 5.0
180.0 becomes 540.0; distance 175.0
185.0 becomes 185.0; distance 180.0
350.0 becomes 350.0; distance 15.0
-10.0 becomes 350.0; distance 15.0
715.0 becomes 355.0; distance 10.0
-700.0 becomes 380.0; distance 15.0
From 700.0:
2.0 becomes 722.0; distance 22.0
10.0 becomes 730.0; distance 30.0
180.0 becomes 540.0; distance 160.0
185.0 becomes 545.0; distance 155.0
350.0 becomes 710.0; distance 10.0
-10.0 becomes 710.0; distance 10.0
715.0 becomes 715.0; distance 15.0
-700.0 becomes 740.0; distance 40.0
(Edited to account for negative ending values)
Edit: From your comment about keeping a second value around, what about setting Location.facing to the adjusted angle, and then adding to Location something like
var prettyFacing: Angle {
var facing = self.facing
while facing.degrees < 0 { facing += Angle(degrees: 360) }
while facing.degrees > 360 { facing -= Angle(degrees: 360) }
return facing
}
After trying both of the other options, we were still getting visual glitches (less common, but still there!).
Our Solution: Use UIKit for Animation
We've created a SPM package that adds a simple modifier, .uiRotationEffect(). This modifier wraps your View in a UIView, and uses UIView's .animate(...) function to get the correct behavior.
You can install the package here or you can just copy and paste the code here, it's not very long.
GIF of the working solution:
I am thinking about using django (2.0.6) with Postgres/postGIS as a backend (10/2.4?).
However, the model geometries I intend to use will not be stored "against" a spheroid (whether earth mars, etc) or any other kind of SRID. Rather a 100% pure geometry in 3D cartesian coordinates, where 1 unit = 1 meter.
How am I to declare model fields and insure they are pure geometry both:
at database level
at application level
Would this work?
geometry = models.MultiPolygonField(_('Geometry'), spatial_index=True, dim=3)
Or better to set the SRID?:
geometry = models.MultiPolygonField(_('Geometry'), spatial_index=True, dim=3, srid=0)
Many thanks
You can use SRID 3857 for 1m Cartesian grid:
models.py:
class Place(models.Model):
geom = models.PointField(srid=3857, dim=3)
def __str__(self):
return str(self.geom.coords)
However, GeoDjango does not support 3d distance calculations out of the box. For example:
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import Distance
from myapp.models import Place
p = Point(100, 100, 0)
Place.objects.create(geom=p)
Place.objects.create(geom=Point(150, 100, 0))
Place.objects.create(geom=Point(100, 100, 22))
Place.objects.create(geom=Point(100, 100, 500))
Place.objects.create(geom=Point(150, 150, 0))
Place.objects.create(geom=Point(250, 250, 0))
for o in Place.objects.filter(geom__distance_lte=(p, Distance(m=100))):
print(o.geom.coords, p.distance(o.geom))
Output:
(100.0, 100.0, 0.0) 0.0
(150.0, 100.0, 0.0) 50.0
(100.0, 100.0, 22.0) 0.0
(100.0, 100.0, 500.0) 0.0
(150.0, 150.0, 0.0) 70.71067811865476
In pure PostGIS, compare ST_Distance and ST_3DDistance :
SELECT *
FROM (
SELECT
"myapp_place"."id",
ST_Distance("myapp_place"."geom",
ST_GeomFromEWKT('SRID=3857;POINT(100 100 0)')) d,
ST_AsEWKT("myapp_place"."geom")
FROM "myapp_place"
) t
WHERE d <= 100.0
vs.
SELECT *
FROM (
SELECT
"myapp_place"."id",
ST_3DDistance("myapp_place"."geom",
ST_GeomFromEWKT('SRID=3857;POINT(100 100 0)')) d,
ST_AsEWKT("myapp_place"."geom")
FROM "myapp_place"
) t
WHERE d <= 100.0
I am trying to subtract some CGFloats, that are defined in this way:
var panelHeight:CGFloat!
When I try to do (screenHeight - panelHeight - tabBarHeight) in the following code, I get an error saying that I cannot apply a binary operator on CGFloat and CGFloat!, which doesn't make much sense, because all three floats are declared with !. If I replace tabBarHeight with tabBarHeight!, then I get an error called "Ambiguous reference to member -".
collisionBehavior.addBoundaryWithIdentifier("upperBoundary" as NSCopying, fromPoint: CGPoint(x: 0, y: screenHeight - panelHeight - tabBarHeight), toPoint: CGPoint(x: boundaryX, y: screenHeight - panelHeight - tabBarHeight))
I can't find the bug report, but this is a known bug. A workaround is to cast the first or the second variable to a non-optional, like so:
(screenHeight as CGFloat) - panelHeight - tabBarHeight
I'm very new to using raphael js library and I'm trying to figure it all out. I'm trying to create a chart based on percentages where 100% would be a full circle. The circle part I have figured out, but how would I go about changing it to show a half-circle for 50% or a quarter of a circle for 25%?
I recommend looking at the code behind this example on the Raphael home page. It should be easy enough to modify it to suit your needs.
This function in particular is what you're looking for
var rad = Math.PI / 180;
function sector(cx, cy, r, startAngle, endAngle, params) {
var x1 = cx + r * Math.cos(-startAngle * rad),
x2 = cx + r * Math.cos(-endAngle * rad),
y1 = cy + r * Math.sin(-startAngle * rad),
y2 = cy + r * Math.sin(-endAngle * rad);
return paper.path(["M", cx, cy, "L", x1, y1, "A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2, "z"]).attr(params);
}
so, a 50% slice would be
var fifty = sector(100,100,50,0,180,{"fill":"red"});
var twentyfive = sector(100,100,50,180,270,{"fill":"red"});
Of course, this is working with degrees - you may want to wrap it so that you can use percentages.
You have to use path() and specify it using SVG's path syntax. Here is an example of creating a closed quarter-circle (upper left quadrant):
var arcPath = paper2.path("M200,200 v-150 a150,150 0 0,0 -150,150 z");
arcPath.attr("fill", "red");
See this link for more on SVG Paths.
i have been do more CGAffineTransform.
how to get current Uiview's angle?
i want to do something when angle==[M_PI/2 ~ M_PI]?
CGFloat radians = atan2f(self.view.transform.b, self.view.transform.a);
CGFloat degrees = radians * (180 / M_PI);
You can check .transfrom property of UIView for that.